log4net not working

后端 未结 13 1661
轮回少年
轮回少年 2020-12-07 08:04

Hey I have this configuration in my web.config


    

        
相关标签:
13条回答
  • 2020-12-07 08:40

    One gotcha for this type of thing is 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.

    0 讨论(0)
  • 2020-12-07 08:46

    Here is my checklist for when log4net is proving to be recalcitrant:

    • ensure that log4net.config file is copied to the bin\ folder when building (set to 'Copy if newer' in compiler)
      • when dealing with installed code, ensure that the log4net.config came along for the ride (set to 'Content' in compiler)
    • ensure that the user that the process runs as has write rights to the folder where the logs are to be written
    • if in doubt, give promiscuous permissions to c:\temp\ and get everything to log to there ()
    • fire up Sysinternal/Dbgview.exe to see if that tells you anything
    0 讨论(0)
  • 2020-12-07 08:46

    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.

    0 讨论(0)
  • 2020-12-07 08:47

    These are the steps which eventually got my file logging working:

    • -Check AssemblyInfo.cs contains the following attribute. [assembly: log4net.Config.XmlConfigurator]. This loads log4net.
    • Check the log directory has write permissions.
    • Check the logger has a format specified. This is done by checking each element in your configuration has a layout element specified. Eg:

    <appender name="MainLogger"... <layout type="log4net.Layout.SimpleLayout"/>

    • Finally, try turning on log4net internal logging to enable console logging and checking the console. To do this, add <add key="log4net.Internal.Debug" value="true"/> to your appSettings.
    0 讨论(0)
  • 2020-12-07 08:49

    There is another small gotcha, see here: http://logging.apache.org/log4net/release/manual/configuration.html#dot-config

    the [assembly: log4net.Config.XmlConfigurator] method doesn't work with app.config. If you configure log4net from app.config, you must use the log4net.Config.XmlConfigurator.Configure() method.

    0 讨论(0)
  • 2020-12-07 08:54

    On my side, I forgot to mark the config file to be copied while the app is being compiled.

    Just right click the log4net.config file, select property and then choose Copy to Output Directory to be Copy XXXX

    0 讨论(0)
提交回复
热议问题