4
votes

I have a .net dll configured as a web service running under IIS. I added log4Net logging and did the below setup under Assembylnfo

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

After that, i enabled the log4net diagnostics using the below in web.config

<system.diagnostics>   
    <trace autoflush="true">
    <listeners>
      <add
        name="textWriterTraceListener"
        type="System.Diagnostics.TextWriterTraceListener"
        initializeData="E:\MyLocation\log4net.txt" />
    </listeners>   
</trace> 
</system.diagnostics>
<system.serviceModel>

Used the below section to create an instance of logger.

private static readonly log4net.ILog logger = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

Now i get the below entries along with a lot of the diagnostics information in my log4net.txt which shows initialisation if fine and it is able to open the log file

log4net: configuring repository [MYRepository] using file [E:\MyLocation\bin\log4net.config] watching for file updates

log4net: Opening file for writing [...]

At this point i am able to get the logs correctly. Now after the logs for initialisation of above, i get the below lines towards the bottom

log4net: configuring repository [MYRepository] using .config file section

log4net: Application config file is [E:\MyLocation\web.config]

log4net:ERROR Failed to find configuration section 'log4net' in the application's .config file. Check your .config file for the <log4net> and <configSections> elements. The configuration section should look like: <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />

If the initialisation was successful as in in the top part of the log, why is log4net trying to check web.config for the configuration again in web.config?

2
Have you enabled <add key="log4net.Internal.Debug" value="true"/> in app.config? Actually, you are trying to do the different things, [assembly: log4net.Config.XmlConfigurator(ConfigFile = "bin\log4net.config", Watch = true)] here you are configuring logs for your application, system.diagnostics enables the log4net internal debugging to monitor the problems of log4net itselfPavel Anikhouski
@asb Try the below Answers and thanksBibin

2 Answers

1
votes

Include the log4net in your project, you can use the NuGet package manager (Alternatively, you can also download and add the log4net.dll reference manually):

PM> install-package Log4Net

Add the following tags to the Web.config file:

<configuration>
  <configSections>
    [...]
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />
  </configSections>
  [...]
   <log4net>
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
  <file value="Logs\FileName.txt"/>
  <appendToFile value="true"/>
  <rollingStyle value="Size"/>
  <maxSizeRollBackups value="5"/>
  <maximumFileSize value="10MB"/>
  <staticLogFileName value="true"/>
  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%date{ISO8601} [%thread] %level %logger %method - %message %exception%newline"/>
  </layout>
  <filter type="log4net.Filter.LevelRangeFilter">
    <levelMin value="DEBUG"/>
    <levelMax value="FATAL"/>
  </filter>
</appender>
<root>
  <level value="ALL"/>
  <appender-ref ref="RollingFileAppender"/>
</root>
<logger>
  <level/>
  <appender-ref/>
</logger>

Add the following line to the AssemblyInfo.cs file:

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

Used the below section to create an instance of logger.

private static readonly log4net.ILog Logger = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
0
votes

Can you show us your log4net.config file?

Please try adding below configuration to you config file and let us know your observations.

 <configSections>
    <!--      Other section configurations go here. For e.g., EF, Elmah..etc    -->
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />

  </configSections>

  <log4net>
    <appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender">
      <param name="File" value="C:\MyWebService.log" />
      <param name="AppendToFile" value="true" />
      <param name="RollingStyle" value="Composite" />
      <param name="DatePattern" value="yyyyMMdd" />
      <param name="MaxSizeRollBackups" value="25" />
      <param name="MaximumFileSize" value="1000KB" />
      <layout type="log4net.Layout.PatternLayout">
        <param name="ConversionPattern" value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
      </layout>
    </appender>
    <root>
      <level value="DEBUG" />
      <appender-ref ref="LogFileAppender" />
    </root>
  </log4net>

If you have web.config file, then I would suggest have these configuration settings in your webservice's web.config file.

On a related note, if this answer does not help you, then please let me know so I would delete the answer and others can see the bounty.