1

never used log4net before, but I may end up kicking myself for that... if I can get it working.

I have the following in my app.config:

<configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>

...

<log4net>
    <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
        <file value="c:\FRLogs\log.txt" />
        <appendToFile value="true" />
        <rollingStyle value="Date" />
        <datePattern value="yyyyMMdd" />
        <layout type="log4net.Layout.PatternLayout">
            <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
        </layout>
    </appender>
    <root>
        <level value="DEBUG" />
        <appender-ref ref="RollingLogFileAppender"/>
    </root>
</log4net>

In my code, I have:

protected static readonly ILog Log = LogManager.GetLogger(typeof(FileRouter));

and then several calls like:

Log.Debug(string.Format("Processing Route {0} for server {1}, source connection {2} ({3})",
            route.RouteID, server.ServerID,
            route.SourceConnection.ConnectionID,
            route.Description));

From what I've read, I should be getting a nice log file in c:\FRLogs\log.txt with all sorts of cool debug information, but the file does not get created, and I do not know what is wrong. My service can write to the directory, so it does not seem (to me) to be a permissions thing.

Philipp M
  • 1,877
  • 7
  • 27
  • 38
Jeremy Holovacs
  • 22,480
  • 33
  • 117
  • 254
  • 1
    you aren't the first - http://stackoverflow.com/questions/1261158/log4net-initialisation ;-) – dash Dec 06 '11 at 21:54
  • Hah not quite the question I asked, but very informative. Adding the assembly tag did the trick. I can request this question be closed, or if you'd like to post as an answer I'll accept it. – Jeremy Holovacs Dec 06 '11 at 22:02
  • We've all been there, don't worry about it. Happy to help. I don't mind, I'll post as an answer and if someone finds the ultimate answer (even if it does require some reading) then everyone should be happy. – dash Dec 06 '11 at 22:04

1 Answers1

4

You've done everything right except initialize the logger. You can do it a few ways, which are usefully documented here:

log4net initialisation

We programmatically initialize it personally, and SO has a great post on this, too:

How to configure log4net programmatically from scratch (no config)

Community
  • 1
  • 1
dash
  • 89,546
  • 4
  • 51
  • 71