Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 class library, I have some lines that call the logger.

I'm trying to read the log4net output data in my Visual Studio 2008 debugging Output window.

Here's my code and my configuration...

//Class Library project
//File: Foo.cs
public class FooService
{
    private static readonly ILog log = LogManager.GetLogger(typeof(FooService));

    public FooService()
    {
        // NOTE: To play with my L4N settings, I'll call Debug once, then Info once.

        log.Info("Starting Constructor");

        // ... snip ...

        log.Debug("Leaving Constructor");
    }
}


// ASP.NET Website project
// File: global.asax
void Application_Start(object sender, EventArgs eventArgs) 
{
    log4net.Config.XmlConfigurator.Configure();
}

// File: Whatever.aspx.cs
// A delegate method (when a user clicks a button) creates the FooService() instance.

// File: web.config
<configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler" requirePermission="false" />

    // ....

</configSections>


<log4net>
    <appender name="ColoredConsoleAppender" type="log4net.Appender.ColoredConsoleAppender">
        <mapping>
            <level value="ERROR" />
            <foreColor value="White" />
            <backColor value="Red, HighIntensity" />
        </mapping>
        <mapping>
            <level value="DEBUG" />
            <backColor value="Green" />
        </mapping>
        <layout type="log4net.Layout.PatternLayout">
            <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
        </layout>
    </appender>

    <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
        <layout type="log4net.Layout.PatternLayout" value="%date [%thread] %-5level %logger - %message%newline" />
    </appender>

    <appender name="OutputDebugStringAppender" type="log4net.Appender.OutputDebugStringAppender">
        <mapping>
            <level value="ERROR" />
            <foreColor value="White" />
            <backColor value="Red, HighIntensity" />
        </mapping>
        <mapping>
            <level value="DEBUG" />
            <backColor value="Blue" />
        </mapping>
        <layout type="log4net.Layout.PatternLayout">
            <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
        </layout>
    </appender>


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

    <!-- Setup the root category, add the appenders and set the default level -->
    <root>
        <level value="ALL" />
        <appender-ref ref="OutputDebugStringAppender" />
        <appender-ref ref="ConsoleAppender" />
        <appender-ref ref="ColoredConsoleAppender" />
    </root>
    <!-- Specify the level for some specific categories -->
    <logger name="DotNetOpenAuth">
        <level value="ALL" />
    </logger>
</log4net>

Cheers for any help or suggestions...

EDIT: Added the RollingLogFileAppender.

like image 661
Pure.Krome Avatar asked Aug 10 '09 09:08

Pure.Krome


2 Answers

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 );
like image 62
kenny Avatar answered Sep 28 '22 09:09

kenny


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"/>
like image 32
Dewfy Avatar answered Sep 28 '22 07:09

Dewfy