Can't get Log4Net to work in my ASP.NET website :(

后端 未结 5 1438
终归单人心
终归单人心 2021-02-10 13:22

really simple question -> i can\'t seem to get any data from Log4Net in my ASP.NET application. I\'ve got a simple ASP.NET website, which references a class library. In this cla

相关标签:
5条回答
  • 2021-02-10 14:06

    I added the following line Global.asax file, and it works..!! log4net.Config.XmlConfigurator.Configure();

    0 讨论(0)
  • 2021-02-10 14:13

    I had this same problem and I think it was looking at the wrong web.config or something. I finally separated out log4net.config from web.config and put a path to it \inetpub\Logs\log4net.config and all is well.

    UDPATED ON REQUEST: edited from a slightly more complicated version.

    <?xml version="1.0" encoding="utf-8"?>
    <log4net>
        <appender name="TraceAppender" type="log4net.Appender.TraceAppender">
            <layout type="log4net.Layout.PatternLayout">
                <conversionPattern value="%date [%thread] %-5level (%logger:%line) - %message%newline" />
            </layout>
        </appender>\
    
        <root>
            <!--ALL, DEBUG, INFO, WARN, ERROR, FATAL, OFF-->
            <level value="ALL" />
            <appender-ref ref="TraceAppender" />
        </root>
    </log4net>
    

    And it is configured in code as follows:

            var logpath = WebConfigurationManager.AppSettings["LogConfigPath"] ?? @"\Inetpub\Logs\log4net.config";
            var finfo = new System.IO.FileInfo ( logpath );
            XmlConfigurator.Configure( finfo );
    
    0 讨论(0)
  • 2021-02-10 14:14

    ASP.Net has restriction on usage of filesystem access, so try to explicitly point directory under App_Data (were it is allowed) Here my working sample:

    <log4net>
        <appender name="FileAppender" type="log4net.Appender.FileAppender">
            <file value="App_Data\logging\log-file.txt"/>
    

    Or with rollover

        <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
            <file value="App_Data\logging\log-append.txt"/>
    
    0 讨论(0)
  • 2021-02-10 14:17

    Ok, found the answer. I needed to use a TraceAppender.

    The application configuration file can be used to control what listeners are actually used. See the MSDN documentation for the Trace class for details on configuring the trace system.

    Events are written using the System.Diagnostics.Trace.Write(string,string) method. The event's logger name is passed as the value for the category name to the Write method.

    Here's my config file data...

    <log4net>
        <appender name="TraceAppender" type="log4net.Appender.TraceAppender">
            <layout type="log4net.Layout.PatternLayout">
                <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
            </layout>
        </appender>
    
    
        <!-- Setup the root category, add the appenders and set the default level -->
        <root>
            <level value="ALL" />
            <appender-ref ref="TraceAppender" />
        </root>
    </log4net>
    
    0 讨论(0)
  • 2021-02-10 14:17

    There are atleast 2 possible problems:

    • The way the address to the file is specified, try using an absolute path instead.
    • The process making the call needs rights to modify the file. Check that the user account making writing to the log file has rights to do so.

    To debug it I would create and empty directory, give everyone full control, configure to log to that directory. Then test it, see that it works, then gradually tighten the security to an acceptable level.

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