views:

361

answers:

3

When I set the file value to 'logs\log-file.txt' where exactly will it create this folder? in the /bin directory?

My web.config looks like:

<log4net>
    <appender name="FileAppender" type="log4net.Appender.FileAppender">
      <file value="logs\log-file.txt" />
      <appendToFile value="true" />
      <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
      </layout>
    </appender>
  </log4net>

is this the correct way to log:

ILog logger = LogManager.GetLogger(typeof(CCController));

logger.Error("Some Page", ex); // where ex is the exception instance

+2  A: 

The file value can either be an absolute path like "c:\logs\log.txt" or a relative path which I believe is relative to the bin directory.

As far as implementing it, I usually place the following at the top of any class I plan to log in:

private static readonly ILog Log = LogManager.GetLogger( 
MethodBase.GetCurrentMethod().DeclaringType);

Finally, you can use it like so:

Log.Debug("This is a DEBUG level message.");
Ben.Vineyard
hmm..it doesn't seem to be creating the log file anywhere, and I am not getting any error?
Blankman
Sounds like a permissions issues. I would map the configuration to an absolute path outside of your web directory. Following that, make sure that the log file and folder have permissions to allow the asp.net worker process proper access.
Ben.Vineyard
A: 

I think your sample is saving to your project folders and unless the default iis, or .NET , user has create permission then it won't be able to create the logs folder.

I'd create the logs folder first and allow the iis user full permission and see if the log file is being created.

griegs
+1  A: 

For the log folder and file stuff, go with @Bens answer.

I will comment on the creating log part, though. Imo there is no correct way. When coding loggers manually I do it the way you're doing it:

ILog logger = LogManager.GetLogger(typeof(CCController));

because it is short and concise.

That said, I do not create the logger instances inside the classes these days, I let my IoC container inject it for me.

Peter Lillevold