Solved

log4net usage

Posted on 2008-10-17
5
564 Views
Last Modified: 2013-12-17
Hi;
I want to use log4net in order to determine the duration of the time of run of a method inside my project.
I have already the code to trace the import of data from a dataset within the class valueaggregator.
My question is how to trace the  method private void exportRunOffSCR within the class Output, that I have instanciated "Ouput outputs = new Output(...) within th class valueaggragator?
Here is my actual working 2 classes "valueaggegator and Output attached.
Thanks
/////////////////////////// CLASS ValueAggregator //////////////////////
 
 
 
using System;
using System.Collections;
using System.IO;
using log4net;
using log4net.Appender;
using log4net.Config;
using log4net.Layout;
using PCModelReporter.Properties;
 
namespace PCModelReporter
{
    internal class ValueAggregator
    {
        private static readonly ILog log = LogManager.GetLogger(typeof (ValueAggregator));
 
        private readonly ArrayList companies;
 
        private readonly String outputDirectoryPath;
        private readonly ArrayList resultSetItems;
 
        public ValueAggregator(ArrayList resultSetItems, String outputDirectoryPath)
        {
            this.resultSetItems = resultSetItems;
            this.outputDirectoryPath = outputDirectoryPath;
            companies = new ArrayList();
        }
 
        public void DoWork()
        {
            try
            {
                PatternLayout layout = new PatternLayout("%date [%level] %logger - %message%newline");
 
                
                FileAppender file = new FileAppender();
                file.Layout = layout;
                file.File = outputDirectoryPath + "\\P&C Model Reporter.log";
                file.AppendToFile = true;
                file.ActivateOptions();
 
                BasicConfigurator.Configure(file);
 
                log.Info("*** Starting ***");
 
                IEnumerator enumResultSetItem = resultSetItems.GetEnumerator();
 
                Hashtable inputList = new Hashtable();
                ResultSetItem resultSetItem;
                Inputs inputs;
 
                int i = 0;
                int nbSubLob = 0;
 
                while (enumResultSetItem.MoveNext())
                {
                    i++;
 
                    resultSetItem = (ResultSetItem) enumResultSetItem.Current;
                    GUIHelper.getInstance().setStatus(i/resultSetItems.Count*100,
                                                      "Excel Input file import : " +
                                                      ((ResultSetItem) enumResultSetItem.Current).companyName);
                    inputs = new Inputs(resultSetItem.inputFile);
                    nbSubLob += inputs.getNbSubLob();
                    inputList.Add(resultSetItem.companyName, inputs);
                }
 
                GUIHelper.init(nbSubLob*2);
 
                GUIHelper.getInstance().setStatus(GUIHelper.percentageInput,
                                                  "Data base results import and aggregation ...");
 
                enumResultSetItem = resultSetItems.GetEnumerator();
                while (enumResultSetItem.MoveNext())
                {
                    resultSetItem = (ResultSetItem) enumResultSetItem.Current;
 
                    GUIHelper.getInstance().setCurrentCompany(resultSetItem.companyName);
 
                    RemetricaHelper.getInstance().setIsDB(resultSetItem.isDB);
 
                    if (resultSetItem.isDB)
                    {
                        RemetricaHelper.getInstance().open(resultSetItem.dbConnection.getConnectionString());
                    }
                    else
                    {
                        RemetricaHelper.getInstance().open(resultSetItem.studyFile);
                    }
                    
                    Constants.resultSetRNWKey = resultSetItem.idResultSetRNW;
 
                    inputs = (Inputs)inputList[resultSetItem.companyName];
                    
                    Constants.percentile = inputs.getValueAtRisk();
 
                    if (inputs.validate())
                    {
                        Company company = new Company(resultSetItem.idResultSetNB, resultSetItem.idResultSetRNW, inputs);
 
                        if (company.validate())
                        {
                            GUIHelper.getInstance().incrementOther("STEC ex post Simulation ...");
 
                         
 
                            GUIHelper.getInstance().incrementOther("Excel output file export ...");
 
                            String outputFilePath = outputDirectoryPath + "\\" + resultSetItem.companyName + "-" +
                                                    DateTime.Now.Year + "-" + DateTime.Now.Month + "-" +
                                                    DateTime.Now.Day + "-" + DateTime.Now.Hour + "-" +
                                                    DateTime.Now.Minute + ".xls";
 
                            File.WriteAllBytes(outputFilePath, Resources.outputs);
 
                            Output outputs = new Output(outputFilePath, company.getResultSetNB(),
                                                        company.getResultSetRNW());
 
                            companies.Add(company);
                        }
 
 
                        else
                        {
                            log.Error("SubLob names are not correclty written in the input file : " +
                                      resultSetItem.inputFile);
                        }
 
                        RemetricaHelper.getInstance().close();
                    }
                    else
                    {
                        log.Error("Input File : " + resultSetItem.inputFile + " is not valid !");
                    }
                }
 
                GUIHelper.getInstance().setStatus(0, "Aggregation successfull !");
 
                GUIHelper.getInstance().setCurrentCompany("");
            }
 
 
            catch (Exception e)
            {
                log.Error("Error during Aggregation", e);
            }
 
         
            log.Info("*** End ***");
        }
    }
}
 
 
 
 
 
 
/////////////////////////// CLASS OUTPUT ////////////////////////////
 
using System;
using System.Collections;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Microsoft.Office.Interop.Excel;
using Application=Microsoft.Office.Interop.Excel.Application;
 
namespace PCModelReporter
{
    internal class Output
    {
        private readonly ResultSet resultSetNB;
 
        private readonly ResultSet resultSetRNW;
 
        private readonly ValueXLOutputHelper xlHelper;
 
 
        public Output(String filePath, ResultSet resultSetNB, ResultSet resultSetRNW)
        {
            xlHelper = ValueXLOutputHelper.getInstance();
 
            this.resultSetNB = resultSetNB;
            this.resultSetRNW = resultSetRNW;
            Application xlApp = new Application();
            xlApp.Visible = false;
 
            Workbook inputs = (xlApp.Workbooks.Open(resultSetNB.getInputs().getFilePath(),
                                                    Type.Missing, Type.Missing, Type.Missing, Type.Missing,
                                                    Type.Missing, Type.Missing, Type.Missing, Type.Missing,
                                                    Type.Missing, Type.Missing, Type.Missing, Type.Missing,
                                                    Type.Missing, Type.Missing));
 
            Workbook workbook = (xlApp.Workbooks.Open(filePath,
                                                      Type.Missing, Type.Missing, Type.Missing, Type.Missing,
                                                      Type.Missing, Type.Missing, Type.Missing, Type.Missing,
                                                      Type.Missing, Type.Missing, Type.Missing, Type.Missing,
                                                      Type.Missing, Type.Missing));
 
            Sheets sheets = workbook.Sheets;
 
            Sheets renewalsSheet = inputs.Sheets;
 
            ((Worksheet) renewalsSheet[1]).Copy(sheets[1], Type.Missing);
 
            inputs.Close(false, Type.Missing, Type.Missing);
 
 
            Worksheet sheet = (Worksheet)sheets.get_Item("SCR Run-Off");
 
            exportRunOffSCR(sheet);
 
         
            workbook.Save();
 
            workbook.Close(false, Type.Missing, Type.Missing);
            Marshal.ReleaseComObject(sheets);
            sheets = null;
            Marshal.ReleaseComObject(sheet);
            sheet = null;
            Marshal.ReleaseComObject(workbook);
            workbook = null;
            xlApp.Quit();
            Marshal.ReleaseComObject(xlApp);
            xlApp = null;
        }
 
 
 
 
 
 
        private void exportRunOffSCR(Worksheet sheet)
        {
         
            xlHelper.setDoubleValue(sheet, ValueXLOutputHelper.rowSCRUndiscountedReserves,
                                    ValueXLOutputHelper.columnSCRFirstDY - 2,
                                    resultSetNB.getPastReservesTotalUndiscountedReserves());
            xlHelper.setDoubleValue(sheet, ValueXLOutputHelper.rowSCRDiscountedReserves,
                                    ValueXLOutputHelper.columnSCRFirstDY - 2,
                                    resultSetNB.getPastReservesTotalDiscountedReserves());
            xlHelper.setDoubleValue(sheet, ValueXLOutputHelper.rowSCRDiversifiedSCR,
                                    ValueXLOutputHelper.columnSCRFirstDY - 2,
                                    resultSetNB.getPastReservesTotalDiversifiedSCR());
            xlHelper.setDoubleValue(sheet, ValueXLOutputHelper.rowSCRRiskOperationalCharge,
                                    ValueXLOutputHelper.columnSCRFirstDY - 2,
                                    resultSetNB.getPastReservesTotalRiskOperationalCharge());
            xlHelper.setDoubleValue(sheet, ValueXLOutputHelper.rowSCRDiversifiedSCRwithRO,
                                    ValueXLOutputHelper.columnSCRFirstDY - 2,
                                    resultSetNB.getPastReservesTotalDiversifiedSCRWithRO());
            xlHelper.setDoubleValue(sheet, ValueXLOutputHelper.rowSCRGrossDiversifiedMVM,
                                    ValueXLOutputHelper.columnSCRFirstDY - 2,
                                    resultSetNB.getPastReservesTotalGrossDiversifiedMVM());
            xlHelper.setDoubleValue(sheet, ValueXLOutputHelper.rowSCRTaxCostOfHoldingCapital,
                                    ValueXLOutputHelper.columnSCRFirstDY - 2,
                                    resultSetNB.getPastReservesTotalTaxCostOfHoldingCapital());
 
            IEnumerator lobEnum = resultSetNB.getLobList().GetEnumerator();
            String stringLOB = null;
 
            int idSubLob = 0;
 
            while (lobEnum.MoveNext())
            {
                IEnumerator subLobEnum = ((LOB)lobEnum.Current).getSubLobList().GetEnumerator();
 
                while (subLobEnum.MoveNext())
                {
                    if (idSubLob > 0)
                    {
                        ValueXLOutputHelper.getInstance().copyTemplateSCRRow(sheet, idSubLob);
                    }
 
                    exportSubLOBRunOffSCR(sheet, idSubLob, (SubLob)subLobEnum.Current);
                    idSubLob++;
                }
 
                exportLOBRunOffSCR(sheet, (LOB)lobEnum.Current);
                stringLOB += ((LOB)lobEnum.Current).getSourceName() + ";";
            }
 
            double[] undiscountedReserves = resultSetNB.getPastReservesUndiscountedReserves();
            double[] discountedReserves = resultSetNB.getPastReservesDiscountedReserves();
            double[] diversifiedSCR = resultSetNB.getPastReservesDiversifiedSCR();
            double[] riskOperationalCharge = resultSetNB.getPastReservesRiskOperationalCharge();
            double[] diversifiedSCRWithRO = resultSetNB.getPastReservesDiversifiedSCRWithRO();
            double[] grossDiversifiedMVM = resultSetNB.getPastReservesGrossDiversifiedMVM();
            double[] taxCostOfHoldingCapital = resultSetNB.getPastReservesTaxCostOfHoldingCapital();
 
           
            for (int i = 0; i < diversifiedSCR.Length; i++)
            {
                xlHelper.setDoubleValue(sheet, ValueXLOutputHelper.rowSCRUndiscountedReserves,
                                        ValueXLOutputHelper.columnSCRFirstDY + i, undiscountedReserves[i]);
                xlHelper.setDoubleValue(sheet, ValueXLOutputHelper.rowSCRDiscountedReserves,
                                        ValueXLOutputHelper.columnSCRFirstDY + i, discountedReserves[i]);
                xlHelper.setDoubleValue(sheet, ValueXLOutputHelper.rowSCRDiversifiedSCR,
                                        ValueXLOutputHelper.columnSCRFirstDY + i, diversifiedSCR[i]);
                xlHelper.setDoubleValue(sheet, ValueXLOutputHelper.rowSCRRiskOperationalCharge,
                                        ValueXLOutputHelper.columnSCRFirstDY + i, riskOperationalCharge[i]);
                xlHelper.setDoubleValue(sheet, ValueXLOutputHelper.rowSCRDiversifiedSCRwithRO,
                                        ValueXLOutputHelper.columnSCRFirstDY + i, diversifiedSCRWithRO[i]);
                xlHelper.setDoubleValue(sheet, ValueXLOutputHelper.rowSCRGrossDiversifiedMVM,
                                        ValueXLOutputHelper.columnSCRFirstDY + i, grossDiversifiedMVM[i]);
                xlHelper.setDoubleValue(sheet, ValueXLOutputHelper.rowSCRTaxCostOfHoldingCapital,
                                        ValueXLOutputHelper.columnSCRFirstDY + i, taxCostOfHoldingCapital[i]);
            }
        }
}

Open in new window

0
Comment
Question by:SirTey
  • 2
5 Comments
 
LVL 37

Accepted Solution

by:
gregoryyoung earned 125 total points
ID: 22742074
why are you using a FileAppender directly? You could just use a named ILog etc in order to do this, it will work but you have hard coded your config information.

As for you original question... just pass it an ILog then have it call the log methods?

Cheers,

Greg
0
 

Author Comment

by:SirTey
ID: 22742514
Hi;  fileappender is used  to store the data within a txt file.
So is this the solution to my problem?:
 adding to Output:

using log4net;
using log4net.Appender;
using log4net.Layout;

 private static readonly ILog log = LogManager.GetLogger(typeof (Output));

log.Info("*** Start of Run Off ***");
private void runoff(){}
log.Info("*** End of Run Off ***");
0
 

Author Comment

by:SirTey
ID: 22742704
I'm thinking to put an event indicating the start of a the method runoffscr in output and and an eventhandler in value aggregator.
0

Featured Post

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
VB.NET 2008 - Windows Protected Your PC 7 42
Convert datetime to time string 10 28
Sort GridView by ID Descending 1 17
abstract class C# 1 31
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question