1

I am writing logs in log file using Log4Net and stored it in local temp folder location (C:\temp..). Now I want to change the text (log) file name at Run time so I have done some code changes on my config file as well as in my application also. But The file name could not be changed on the local temp folder and the log also didn't get written on the text file.

Can you please anyone tell me the solution of this problem or tell me some other examples to work out this problem?

Here My code is:

My Config File(Web.Config) settings :

<appender name="RollingPatternFileAppender"  type="log4net.Appender.RollingFileAppender">
    <file type="log4net.Util.PatternString" value="%property{LogFileName}.log" />
    <appendToFile value="true" />
    <rollingStyle value="Size" />
    <maxSizeRollBackups value="10" />
    <maximumFileSize value="100MB" />
    <staticLogFileName value="false" />
    <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date{yyyy-MM-dd HH:mm:ss} %-5level : [%logger] - %message %newline" />
    </layout>
</appender>
<logger name="SPM.SERVER">
    <level value="DEBUG" />
    <appender-ref ref="RollingPatternFileAppender" />
</logger>

My Application Code is:

ILog logger = LogManager.GetLogger("SPM.SERVER");
log4net.ThreadContext.Properties["LogFileName"] = "MyLog";
logger.Debug("Load Data",ex);
XmlConfigurator.Configure();

This code didn't write the log inside of the file and didn't change the file name as well. Anyone please correct what I did wrong in this code or tell me some other examples to achieve this solution.

stuartd
  • 70,509
  • 14
  • 132
  • 163
SuryaKavitha
  • 493
  • 5
  • 16
  • 36
  • possible duplicate of [Configuring log4net appenders via XML file *and* code](http://stackoverflow.com/questions/2807334/configuring-log4net-appenders-via-xml-file-and-code) –  May 24 '12 at 08:13
  • @AndreasNiedermair: I am not able to open that website. – SuryaKavitha May 24 '12 at 08:14
  • By what do you want to change the file name? Log4net is pretty flexible and you could use different appenders. Why do you need c# for this? – nunespascal May 24 '12 at 08:16
  • @nunespascal: I am new to this Log4net concept. Can you help me Which type of appender i have to use for this implementation? and Can you please give me a bit more explanation about your point of view implementation? – SuryaKavitha May 24 '12 at 08:30

1 Answers1

2

log4net allows you to have multiple appenders. You can configure each to listen only to a particular set of messages.

Take a look at this very good set of documentation. It has a lot of examples on how to do things.

Here is a rather rough example:

<log4net>
  <appender name="A1" type="log4net.Appender.RollingFileAppender">
    <file value="Errors" />
    <appendToFile value="true" />
    <rollingStyle value="Date" />
    <staticLogFileName value="false" />
    <param name="DatePattern" value="dd.MM.yyyy'.txt'" />
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%date [%thread] %-5level %logger - %message%newline" />
    </layout>
  </appender>
  <appender name="A2" type="log4net.Appender.RollingFileAppender">
    <evaluator type="log4net.Core.LevelEvaluator">
      <threshold value="DEBUG"/>
    </evaluator>
    <filter type="log4net.Filter.StringMatchFilter">
      <stringToMatch value="Some special message" />
      <acceptOnMatch value="true" />
    </filter>
    <filter type="log4net.Filter.DenyAllFilter" />
    <file value="Debug" />
    <appendToFile value="true" />
    <rollingStyle value="Date" />
    <staticLogFileName value="false" />
    <param name="DatePattern" value="dd.MM.yyyy'.txt'" />
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%date [%thread] %-5level %logger - %message%newline" />
    </layout>
  </appender>
  <root>
    <appender-ref ref="A1" />
    <appender-ref ref="A2" />
  </root>
</log4net>
Philipp M
  • 1,877
  • 7
  • 27
  • 38
nunespascal
  • 17,584
  • 2
  • 43
  • 46
  • Thanks for your code. I changed your code on my config settings How can i proceed in my application and How can i assign the File location and file name on Web.config file and My Application. Can you please tell me about it? – SuryaKavitha May 24 '12 at 10:55
  • 1
    The file attribute does that. I have used a relative path, but you can use absolute paths too. Rolling file appender adds the rest of the part of file name depending upon the pattern you specify. – nunespascal May 24 '12 at 11:38