Solved

dynamic log file name with log4net

Posted on 2010-11-23
3
3,417 Views
Last Modified: 2013-12-17
I am using log4net to log information, errors and warnings while running my console ETL application in C#.

I am generating a new log file daily (so the file name just consist of the date)

however due to change in requirements I am now required to dynamically generate a new log (which is a combination of Filename to be extracted and Datetime)

I tried changing my currently log4net settings in the App.config file without much success.

Here is my new log4net settings that tries to generate a new log file for each extract

<appender name="LogFileAppender" type="log4net.Appender.FileAppender">
     <param name="File" value="\\\\NetworkPath\\Logs\\%property{LogName}"/>
     <param name="AppendToFile" value="true"/>
     <rollingStyle value="Size" />
     <maxSizeRollBackups value="-1" />
     <maximumFileSize value="5000KB" />
     <countDirection value="1"/>
     <layout type="log4net.Layout.PatternLayout">
       <param name="Header" value="[Header]\r\n"/>
       <param name="Footer" value="[Footer]\r\n"/>
       <param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss} %p %u %c %l %m %n"/>
     </layout>
     <filter type="log4net.Filter.PropertyFilter">
       <Key value="Version" />
       <StringToMatch value="1" />
     </filter>
     <filter type="log4net.Filter.DenyAllFilter" />
   </appender>

*** original code replaced by thermoduric at askers request {http://www.experts-exchange.com/Q_27105761.html}.

and in the actual c# file i try to implement the log file is as follows

                foreach (var file in files)
                {
                    //GlobalContext.Properties["LogName"] = config.Source + " - " + config.SqlMappingCode + ".log";
                    GlobalContext.Properties["LogName"] = "test.log";
                    var _log = LogManager.GetLogger(typeof(Program));
                    XmlConfigurator.Configure();
                    _log.Info("Start Processing FX Rates feed...");
                    csvImporter.LoadData(fxRatesConfig, JobType.FXRates);
                }

However this code just results in the following file name

%property{LogName}

Can anyone please tell me how i can achieve the dynamic log file?
0
Comment
Question by:2ooth
  • 2
3 Comments
 
LVL 5

Accepted Solution

by:
buraksarica earned 500 total points
ID: 34195397
Check this blog post : http://geekswithblogs.net/rgupta/archive/2009/03/03/dynamic-log-filenames-with-log4net.aspx

It seems that you are working with wrong type of Appender.
0
 
LVL 5

Expert Comment

by:buraksarica
ID: 34203208
Is it working?
0
 

Author Comment

by:2ooth
ID: 34203919
Yes it is working!!!!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
I designed this idea while studying technology in the classroom.  This is a semester long project.  Students are asked to take photographs on a specific topic which they find meaningful, it can be a place or situation such as travel or homelessness.…

919 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now