views:

2074

answers:

5

Hi folks,

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.

+2  A: 

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"/>
Dewfy
Heh - dude. I never said i'm using the file system :) go look again .....
Pure.Krome
Just try this working sample, after it you could separate not working configuration from not tuned security.
Dewfy
ok.. i'll give this a go... hmm. some errors in the OUTPUT window...og4net:ERROR XmlHierarchyConfigurator: No appender named [RollingFileAppender] could be found.log4net:ERROR XmlHierarchyConfigurator: Appender named [RollingFileAppender] not found.log4net:ERROR XmlHierarchyConfigurator: Cannot find Property [mapping] to set object on [log4net.Appender.OutputDebugStringAppender]log4net:ERROR XmlHierarchyConfigurator: Cannot find Property [mapping] to set object on [log4net.Appender.OutputDebugStringAppender]
Pure.Krome
I'm not persist use RollingLogFileAppender, try just FileAppender, also look at my section from <configSection> <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
Dewfy
+1  A: 

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 );
kenny
dude - did u end up logging to the visual studio (debugger) OUTPUT window? or to a file?
Pure.Krome
yeah, I'm using TraceAppender with goes to the OutputDebugString facility and I view it in VS or with http://technet.microsoft.com/en-us/sysinternals/bb896647.aspx
kenny
wow. that sysinternals app is awesome :) i love there stuff and here's another kew thing :) Can u please elaborate in detail (eg. post a screen shot of your .config file log4net settings), please?
Pure.Krome
@Pure, I dig the Melbourne shuffle dude!!
kenny
*wink wink* gotta love chicks shufflin.... unless they are fat .... doesn't look good, then.
Pure.Krome
A: 

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.

Shiraz Bhaiji
+3  A: 

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>
Pure.Krome
+1- as you have said this works!
RichardOD
A: 

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

Shan
Er ... I had that already listed in my opening post. That said, I've dropped using Log4Net and i'm now using NLog .. it's SOOOO much better IMO :) And it's always worked (plus it's newer and is still getting development-love).
Pure.Krome