0

I am using log4net to log the error in my application.But the log file is not getting created.As far as I can see,the code is fine.

Web.config code

 <configSections>
    <!-- For more information on Entity Framework configuration, visit  http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
</configSections>
<log4net>
<appender name="Console" type="log4net.Appender.ConsoleAppender">
    <layout type="log4net.Layout.PatternLayout">
        <!-- Pattern to output the caller's file name and line number -->
        <conversionPattern value="%5level [%thread] (%file:%line) - %message%newline" />
    </layout>
</appender>
<appender name="RollingFile" type="log4net.Appender.RollingFileAppender">
    <file value="C:\Demo\example.log" />
    <appendToFile value="true" />
    <maximumFileSize value="100KB" />
    <maxSizeRollBackups value="2" />
    <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%level %thread %logger - %message%newline" />
    </layout>
</appender>
<root>
    <level value="DEBUG" />
    <appender-ref ref="Console" />
    <appender-ref ref="RollingFile" />
</root>
</log4net>

In the AssemblyInfo.cs file,I have added the following code

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

In the class where I want to track the errors,I have added the following code

  private static readonly ILog logger = LogManager.GetLogger(typeof(ProviderRepository));

I am using the logger as so in the catch block.

 logger.Error(Ex.Message);

But I dont see the Log file.Am I doing something wrong here?

Shanks
  • 89
  • 1
  • 14
  • Try private static readonly ILog _log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); – Ryan Jan 22 '15 at 02:47
  • Also try separating the config to a separate log4net.config file and adjusting the declaration in the AssemblyInfo - I think this worked for me. – Ryan Jan 22 '15 at 02:48
  • I made the changes mentioned by you.Unfortunately,I am still facing the same issue. – Shanks Jan 22 '15 at 03:12
  • Any other suggestions? .. I need to finish a DemoApp.Time is of the essence for me.Thanks a lot for your reply. – Shanks Jan 22 '15 at 05:08
  • Do you see logs in the console? Are you sure you are going in the catch block (add a log.Debug at the start of your method and check in the Console)? – samy Jan 22 '15 at 08:19
  • If you use assembly attributes you must have a call to initialise the log manager in the startup program of the application: "if you use configuration attributes you must invoke log4net to allow it to read the attributes. A simple call to LogManager.GetLogger will cause the attributes on the calling assembly to be read and processed. **Therefore it is imperative to make a logging call as early as possible during the application start-up, and certainly before any external assemblies have been loaded and invoked"** - http://logging.apache.org/log4net/release/manual/configuration.html#attributes – stuartd Jan 22 '15 at 09:54
  • @stuartd i'm leaning more on a simple mistake, like just not going through the catch block (hence the log.debug call), or not having permissions on the file directory (hence the console check). But it never hurts reminding what you said :) – samy Jan 22 '15 at 11:21
  • possible duplicate of [log4net not working](http://stackoverflow.com/questions/3898218/log4net-not-working) – Mr Mush Jan 27 '15 at 20:20

2 Answers2

0

You should check if your apppool user has access to C:\Demo\, next you can enable log4net internal debugging which will tell you if there is any problem in your configuration or file access etc:

<configuration>
...
    <appSettings>
        <add key="log4net.Internal.Debug" value="true"/>
    </appSettings>

...

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

Log4net FAQ

Peter
  • 27,590
  • 8
  • 64
  • 84
0

Thanks a lot for all your suggestions guys.I was able to figure this one out.I had write the below line of code in the Global.asax file

[assembly: log4net.Config.XmlConfigurator(Watch = true)]

I was writing it earlier in the Assembly.info.cs file.

Shanks
  • 89
  • 1
  • 14