Solved

dynamic log file name with log4net

Posted on 2010-11-23
3
3,362 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
Comment Utility
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
Comment Utility
Is it working?
0
 

Author Comment

by:2ooth
Comment Utility
Yes it is working!!!!
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

743 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

18 Experts available now in Live!

Get 1:1 Help Now