Click here to Skip to main content
Email Password   helpLost your password?

Introduction

Download Sample web.Config - 1.2 Kb

Log4net is an open source utility used for log/report statements to various kinds of output targets (Text file, Email, Database, Event Viewer etc.)

http://logging.apache.org/

 

It can be a very handy utility for application instrumentation purpose.

 

 

Configuring Log4net:

 

Step 1: Add a reference of Log4net.dll to the project.

 

Step 2: Add Global.asax to the project, if not already added. In the �Application_Start� event of global.asax add following code:

 

log4net.Config.XmlConfigurator.Configure();

 

Step 3: In web.config file, under Configuration->Configsections add following section:

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

 

Step 4: In web.config, add new section �<log4net>�. This section will contain all the settings related to log4net configuration.

 

Step 5: In web.config, under �log4net� section, add required appenders (output target) configuration sections in the following way:

         

         

File Appender configuration, used for log statements in a text file.       

<appender name="LogFileAppender" type="log4net.Appender.FileAppender">

<param name="File" value="Logs\\Log4Net.log"/>

<layout type="log4net.Layout.PatternLayout">

<param name="ConversionPattern" value="%d [%t] %-5p %c %m%n"/>

</layout>

</appender>

 

          Smtp Appender configuration, used for log statements by sending email.

<appender name="SmtpAppender" type="log4net.Appender.SmtpAppender">

<to value="" />

<from value="" />

<subject value="" />

<smtpHost value="" />

<bufferSize value="512" />

<lossy value="true" />

<evaluator type="log4net.Core.LevelEvaluator">

<threshold value="WARN"/>

</evaluator>

<layout type="log4net.Layout.PatternLayout">

<conversionPattern value="%newline%date [%thread] %-5level %logger [%property] - %message%newline%newline%newline" />

</layout>

</appender>

 

Step 6: In web.config file, under �log4net� section, for each appender, add loggers in the following way:

 

<logger name="File">

<level value="All" />

<appender-ref ref="LogFileAppender" />

</logger>

<logger name="EmailLog">

<level value="All" />

<appender-ref ref="SmtpAppender" />

</logger>

          ���

          ���

 

Step 7: Now whenever we want to log any information/error/warning, call appropriate method in the following manner:

 

          //for logging to file

          log4net.ILog logger = log4net.LogManager.GetLogger("File");

 

          //for emailing

          log4net.ILog logger = log4net.LogManager.GetLogger("EmailLog");

 

 

          logger.Info(�Starting page load�);

           

 

You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
GeneralUser Level Logging
Member 3679438
17:11 26 Nov '09  
I am using log4net in my apolication but I need to log for individual users who ever acess my web site. I tried to create log files where file name would given dynamically when user logged in. But when one user is active and if the second user logs in, the logs gets appended to first logger and failes to create a new file. Please help.
GeneralBlank Page
dhaval.upadhyay
2:17 9 Feb '09  
Hi I have set as you have suggested but I am getting blank log file.
Am i missing some thing?

I am also not accessing this file untill Restarting the visual stodio.
(Message Another Application is using file)
Generallog4net is not working with Access and SQL-Server using ASP.NET
Bimal Kothari
5:33 13 Aug '08  
Hi,

Can I have complete sample to insert log data in to Access and SQL-Server.

I have verified that log4net is not working with Access and SQL-Server using ASP.NET after making all
configuration correct and proper.

Please provide me complete sample if it possible soon.

Thanks,
Bimal
Generallog4net is not working with Access and SQL-Server
Bimal Kothari
5:32 13 Aug '08  
Hi,

Can I have complete sample to insert log data in to Access and SQL-Server.

I have verified that log4net is not working with Access and SQL-Server using ASP.NET after making all
configuration correct and proper.

1.
Here is my config file:
------------------------------------------------------------------------------------------------

<!-- This section contains the log4net configuration settings -->
<!--Levels - OFF, ALL, WARN, FATAL, INFO, ERRROR, DEBUG -->


------------------------------------------------------------------------------------------------

Please provide me complete sample if it possible soon.

Thanks,
Bimal
GeneralNeed log4net Solution and sample for Access and SQL-Server
Bimal Kothari
5:30 13 Aug '08  
Hi,

Can I have complete sample to insert log data in to Access and SQL-Server.

I have verified that log4net is not working with Access and SQL-Server using ASP.NET after making all
configuration correct and proper.

Please provide me complete sample if it possible soon.

Thanks,
Bimal
GeneralSecurity Exception
Manojkumar Sharma
1:49 6 Jun '08  
I am facing security exception in asp.net 2.0. Below is the error
Description: The application attempted to perform an operation not allowed by the security policy. To grant this application the required permission please contact your system administrator or change the application's trust level in the configuration file.

Exception Details: System.Security.SecurityException: Request for the permission of type 'System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.

Source Error:


Line 5: void Application_Start(object sender, EventArgs e)
Line 6: {
Line 7: log4net.Config.XmlConfigurator.Configure();
Line 8:
Line 9: }

Can anyone pls guide?
Generalhi
manoj22184
3:59 9 May '08  
Thanks for your code and explanation but can you please tell me some more clear please i dnt have somuch experience please helpm me regarding this.

Thank you in advance

M.MANOJ

manoj22184@gmail.com
GeneralThanks !
andy_sinclair
6:49 1 Nov '07  
I was using various bits and pieces from other sites to try and get logging of components working within ASP.NET. Nothing was working, but your checklist (especially the global.asax) did the trick !

Andy
GeneralThank you, it's helpful!
liuruicai
17:21 11 Oct '07  
Thank you, it's helpful!
Generalblank file
RADman
5:26 10 Oct '07  
Hello,

I have tried to implement this solution and I do get an empty log file in my logs folder, but nothing ever gets written to it, can you perhaps hit the main items that I need to investigate to make sure I have it configured correctly?

Thanks.
GeneralWhere should i add the new section
satyamythily
7:00 26 Jul '07  
Hi,
where should i add a new section. please help me at point numner 4...i mean under which configuration i have to add the . can u please explain me with an example so that it will be helpful for me,
if i add it under the it is giving me the error. where ever it is giving me the error.
Thanks
mythily
GeneralRe: Where should i add the new section [modified]
Noman Mohammad
7:33 6 Aug '07  
Firstly you have to add a section under configSections. If you dont have the configSections then add it right after configuration. Example:

<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/<
</configSections&;gt
Once you have added that section then you add <log4net> section just after appSettings and connectionString for example:

<appSettings>
your code...
</appSettings>
<connectionString> -- if you have this section add after this or just after appsettings
</connectionString>

Now ADD:
<log4net>
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="D:\example.log"/>
<appendToFile value="true"/>
<rollingStyle value="Date"/>
<datePattern value="yyyyMMdd"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger - %message%newline"/>
</layout>
</appender>

<root>
<level value="DEBUG"/>
< </root>
</log4net>

Simple!Smile

Nom84



-- modified at 12:43 Monday 6th August, 2007
GeneralNice
nsebastian
12:47 20 Jun '07  
Your article help me a lot. You must a god damn genious
GeneralBest log4net guide I found
reach4thelasers
7:57 28 May '07  
Hey, thanks for this! Very well written and easy to follow - even for a complete beginner. I've seen a few log4net guides on code project, I think this is by far the best one.

Thanks again,

Kevin.
Questionlogs
LeleHalfon
7:55 8 May '07  
where is the folder Logs ?
should i create it?
GeneralThank-you!
Garbage Can
14:00 10 Dec '06  
Thank-you for this article. It was difficult to quickly find info on setting up Log4Net within .NET 2 but your article efficiently explained the process. Thank-you and very much appreciated.
GeneralXmlConfiguratorAttributte
TomO2
4:57 17 Oct '06  
Another way to start up logging is to use log4net.Config.XmlConfiguratorAttribute:

[assembly : XmlConfigurator(ConfigFile = "Log4Net.config", Watch = true)]
With this attribute in your application (I put it in the AssemblyInfo.cs), log4net will use the file Log4Net.config instead of web.config. The Watch=true tells log4net to keep an eye on the file for changes.
GeneralRe: XmlConfiguratorAttributte
dnh
5:28 11 Apr '07  
Very Cool


"Throughout human history, we have been dependent on machines to survive. Fate, it seems, is not without a sense of irony. " - Morpheus
"Real men use mspaint for writing code and notepad for designing graphics." - Anna-Jayne Metcalfe

GeneralRe: XmlConfiguratorAttributte
todeti
7:44 23 Oct '07  
But we can't find assemblyinfo file in .net 2.0
GeneralClosing log files
Dejan Grujic
23:29 16 Oct '06  
This is all basic log4net setting. You've missed one important thing that's not very well documented and therefore much more useful:

protected void Application_End(Object sender, EventArgs e)
{
LogManager.Shutdown();
}

Before I added this I had cases when logs were not working any more after web application was restarted because log files were not unlocked properly.
GeneralRe: Closing log files
Vikramaditya S Shekhawat
23:32 16 Oct '06  
I never faced this problem, but its always good to have such clean up code.
Thanks DejanSmile
GeneralDirectory write permissions
Rodrigo Dias
2:36 16 Oct '06  
By the way, you have to add write permissions to the destination directory is the asp.net folder for the FileAppenders, i.e., in your configuration you are writing the log files to the directory "Logs". You have to add write permissions to the directory for the ASP.NET user.
At least for me i had to do that....
GeneralRe: Directory write permissions
Vikramaditya S Shekhawat
2:39 16 Oct '06  
yes,
you are correct.
GeneralRe: Directory write permissions
KrishanChaminda
15:11 19 Oct '06  
Hi

Has anyone used a network path to store the Log?. I need to use a network location. i hav given rights to this folder but i still cant write a log entry.

Cheers
cK

Generalweb.config
Vladimir_V
9:05 13 Oct '06  
Could you attach configured Web.config file please.

Thanks,
Vladimir


Last Updated 11 Oct 2006 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2010