?
Solved

dynamic log file name with log4net

Posted on 2010-11-23
3
Medium Priority
?
3,840 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 5

Accepted Solution

by:
buraksarica earned 2000 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

Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

Question has a verified solution.

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

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

649 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