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 this:
<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
Log4net is saving into your project folder. Something like:
\SolutionFolder\ProjectFolder\bin\SolutionConfiguration\logs\log-file.txt
.Where:
Hope this helps!
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:
Finally, you can use it like so:
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.
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:
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.
it will create the file in the root directory of your project/solution.
You can specify a location of choice in the web.config of your app as follows:
the file tag specifies the location.