Solved

dynamic log file name with log4net

Posted on 2010-11-23
3
3,450 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

776 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