Use log4net with separate config file

1.1k views Asked by At

my AssemblyInfo.cs:

[assembly: log4net.Config.XmlConfigurator(ConfigFile = "Log4Net.config", Watch = true)]

i change the structure just like you said

(i just change the <file value="..\Logs\\CurrentLog" /> to <file value="D:\Log\Log.txt" />):

<?xml version="1.0" encoding="UTF-8"?>
<log4net>
  <root>
    <level value="ALL" />
    <appender-ref ref="RollingFileAppender"/>
  </root>
  <appender name="RollingLogFileAppender"  type="log4net.Appender.RollingFileAppender">
    <file value="D:\Log\Log.txt" />
    <appendToFile value="true" />
    <datePattern value="yyyyMMdd" />

    <rollingStyle value="Date" />
    <filter type="log4net.Filter.LevelRangeFilter">
      <acceptOnMatch value="true" />

      <levelMin value="INFO" />
      <levelMax value="FATAL" />
    </filter>

    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern
      value="%-5p %d %5rms %-22.22c{1} %-18.18M - %m%n" />
    </layout>
  </appender>
</log4net>

and define the log:

private static readonly ILog Log = LogManager.GetLogger("Logger");

but still the log.txt file doesn't created yet!

2

There are 2 answers

0
Cheshire Cat On BEST ANSWER

There's something missing in your config file.

<log4net>
  <logger name="default">
    <level value="ALL"/>
  </logger>

  <root>
    <level value="ALL" />
    <appender-ref ref="RollingLogFileAppender" />
  </root>

  <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
    <file value="..\Logs\\CurrentLog" />
    <appendToFile value="true" />
    <datePattern value="yyyyMMdd" />
    <rollingStyle value="Date" />
    <filter type="log4net.Filter.LevelRangeFilter">
      <acceptOnMatch value="true" />
      <levelMin value="INFO" />
      <levelMax value="FATAL" />
    </filter>
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern 
      value="%-5p %d %5rms %-22.22c{1} %-18.18M - %m%n" />
    </layout>
  </appender>
</log4net>

And also the declaration:

private static readonly ILog Log = LogManager.GetLogger("default");
2
Yeldar Kurmangaliyev On

Actually, you haven't declared a logger in your config and it has wrong structure. This is how it should look like:

<?xml version="1.0" encoding="UTF-8"?>
<log4net>
    <logger name="Logger">
        <level value="ALL" />
        <appender-ref ref="RollingFileAppender"/>
    </logger>
    <appender name="RollingLogFileAppender"  type="log4net.Appender.RollingFileAppender">
        <file value="..\Logs\\CurrentLog" />
        <appendToFile value="true" />
        <datePattern value="yyyyMMdd" />

        <rollingStyle value="Date" />
        <filter type="log4net.Filter.LevelRangeFilter">
            <acceptOnMatch value="true" />

            <levelMin value="INFO" />
            <levelMax value="FATAL" />
        </filter>

        <layout type="log4net.Layout.PatternLayout">
            <conversionPattern 
            value="%-5p %d %5rms %-22.22c{1} %-18.18M - %m%n" />
        </layout>
    </appender>
</log4net>

Now you can access it this way:

private static readonly ILog Log = LogManager.GetLogger("Logger");

It definitely works.

Now about your code snippet. I'm not sure about your initialization. I haven't worked with log4net this way:

private static readonly ILog Log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

However, you can try declaring logger in root and initialize it:

<?xml version="1.0" encoding="UTF-8"?>
<log4net>
    <root>
        <level value="ALL" />
        <appender-ref ref="RollingFileAppender"/>
    </root>
    <appender name="RollingLogFileAppender"  type="log4net.Appender.RollingFileAppender">
        <file value="..\Logs\\CurrentLog" />
        <appendToFile value="true" />
        <datePattern value="yyyyMMdd" />

        <rollingStyle value="Date" />
        <filter type="log4net.Filter.LevelRangeFilter">
            <acceptOnMatch value="true" />

            <levelMin value="INFO" />
            <levelMax value="FATAL" />
        </filter>

        <layout type="log4net.Layout.PatternLayout">
            <conversionPattern 
            value="%-5p %d %5rms %-22.22c{1} %-18.18M - %m%n" />
        </layout>
    </appender>
</log4net>

// ...
private static readonly ILog Log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);