views:

78

answers:

4

Hey I have this configuration in my web.config

<log4net>
<appender name="LogFileAppender" type="log4net.Appender.FileAppender">
    <param name="File" value="mylog.log" />
    <param name="AppendToFile" value="true" />
    <layout type="log4net.Layout.PatternLayout">
        <param name="Header" value="" />
        <param name="Footer" value="" />
        <param name="ConversionPattern" value="%d [%t] %-5p %m%n" />
    </layout>
</appender>
<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender" >
    <layout type="log4net.Layout.PatternLayout">
        <param name="Header" value="[Header]\r\n" />
        <param name="Footer" value="[Footer]\r\n" />
        <param name="ConversionPattern" value="%d [%t] %-5p %m%n" />
    </layout>
</appender>
<root>
    <level value="DEBUG" />
    <appender-ref ref="LogFileAppender" />
    <appender-ref ref="ConsoleAppender" />
</root>

but log4net is not working. My project compiles fine, and I get no errors debugging either. The lines where I tell to log.debug("somemessage") gets run fine, but i cant find the mylog.log file, so where is it?

+5  A: 

I haven't used the framework in a while, so I don't know if this has changed in recent years. But one gotcha for this type of thing was to make sure to add the XmlConfigurator attribute to the assembly by placing the following line in your AssemblyInfo.cs:

[assembly: log4net.Config.XmlConfigurator()]

Otherwise log4net never activates.

Kirk Woll
that was AWESOME!!
Jakob
+1  A: 

I've had experiences where logging systems fail silently without raising exceptions. I suppose this makes sense because if the logger is logging errors then how can it log an error that it's unable to perform logging?

So if the file isn't created on disk, start by looking into file system permissions to ensure the user your application is running under can write a new file to that disk location.

For testing purposes you might want to manually create the file on disk that should be written to and open up permissions for everybody to write to it. If the logger starts writing to it then you know it's permission based rather than configuration based.

John K
+3  A: 

I guess that either log4net is not logging at all, or the file is not ending up where you expect it.

Firstly, have you actually called

XmlConfigurator.Configure()

anywhere in your code? If the xml snippet above is in the application configuration file, this call will do the trick. If the xml snippet is in it's own file, you'll need to use the .Configure(string) overload that takes the path to the file. Without this call (or apparently the assembly level attribute mentioned by Kirk Woll), then log4net won't be logging at all.

If you believe this is all done, and log4net should be logging, then maybe you should put a fully qualified path in for the log file while you debug further. That will let you be sure where the file should be.

Rob Levine
A: 
<param name="File" value="mylog.log" />

is saying "write mylog.log to actual folder". This means that if your webapp is under IIS, then log will be written to C:\inetpub\wwwroot\appname\mylog.log.

If log file is not there, then maybe account under which is app running doesn't have write permission on folder. You can run Process Monitor from SysInternals to see if and where a file is written.

Also run VS in debug mode, to see if any exceptions are thrown (Debug->Exceptions->CLR Exceptions, check Thrown).

Tomas Voracek