flynny
asked on
log4net help
Hi all,
I am trying to use the log4net to create logs for my application. I want to append debug messages and errors to both the console and a rolling log file.
Now I am having issues getting it set up. I am working from the tutorial here
http://www.codeproject.com/Articles/140911/log-net-Tutorial
I have copied the .dll to my bin directory of my web application.
I have the following code in my web.config;
I then have an extended usercontrol class, which I add the following line (to save me adding to every ascx file).
Now when I try to log a bit of info, i.e.;
the file is not being created.
Could someone please advise me on what I am doing wrong here?
I am trying to use the log4net to create logs for my application. I want to append debug messages and errors to both the console and a rolling log file.
Now I am having issues getting it set up. I am working from the tutorial here
http://www.codeproject.com/Articles/140911/log-net-Tutorial
I have copied the .dll to my bin directory of my web application.
I have the following code in my web.config;
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>
<log4net>
<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date{ABSOLUTE} [%thread] %level %logger - %message%newlineExtra" />
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="INFO" />
<levelMax value="FATAL" />
</filter>
</appender>
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="~/Logs/errorlogfile.txt" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="5" />
<maximumFileSize value="10MB" />
<staticLogFileName value="true" />
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<filter type="log4net.Filter.StringMatchFilter">
<stringToMatch value="debug" />
</filter>
<filter type="log4net.Filter.StringMatchFilter">
<stringToMatch value="error" />
</filter>
<filter type="log4net.Filter.DenyAllFilter" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %level %logger - %message%newline%exception" />
</layout>
</appender>
<root>
<level value="INFO" />
<appender-ref ref="RollingFileAppender" />
<appender-ref ref="ConsoleAppender" />
</root>
</log4net>
I then have an extended usercontrol class, which I add the following line (to save me adding to every ascx file).
public static readonly log4net.ILog log = log4net.LogManager.GetLogger
Now when I try to log a bit of info, i.e.;
log.Info("Info logging");
the file is not being created.
Could someone please advise me on what I am doing wrong here?
ASKER
Sorry I forgot to put add this to my question. Yes I has added the following to my AssemblyInfo.cs file in the root of my project;
Also the reason I change from Private to public was because i have extended the usercontrol class to add the variable in and i got an error on accessing the log variable due to protection levels??
[assembly: log4net.Config.XmlConfigurator(Watch = true)]
Also the reason I change from Private to public was because i have extended the usercontrol class to add the variable in and i got an error on accessing the log variable due to protection levels??
ASKER
Hi,
Thanks for the reply.
Not that it should make a difference but I am working witha web application.
Just to put all the infor together currently;
I have the following in my web.config
then i have the following in my AssemblyInfo.cs
I also have now (fromgoogling) added the following to my global.asax file;
however the above messages are not being outputted to either output window or the text file??
Am I doing something wrong here? (there are no errors being thrown either).
Thanks for the reply.
Not that it should make a difference but I am working witha web application.
Just to put all the infor together currently;
I have the following in my web.config
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>
<log4net>
<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date{ABSOLUTE} [%thread] %level %logger - %message%newlineExtra" />
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="INFO" />
<levelMax value="FATAL" />
</filter>
</appender>
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="~/Logs/errorlogfile.txt" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="5" />
<maximumFileSize value="10MB" />
<staticLogFileName value="true" />
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<filter type="log4net.Filter.StringMatchFilter">
<stringToMatch value="debug" />
</filter>
<filter type="log4net.Filter.StringMatchFilter">
<stringToMatch value="error" />
</filter>
<filter type="log4net.Filter.DenyAllFilter" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %level %logger - %message%newline%exception" />
</layout>
</appender>
<root>
<level value="INFO" />
<appender-ref ref="RollingFileAppender" />
<appender-ref ref="ConsoleAppender" />
</root>
</log4net>
then i have the following in my AssemblyInfo.cs
[assembly: log4net.Config.XmlConfigurator(Watch = true)]
I also have now (fromgoogling) added the following to my global.asax file;
void Application_Start(object sender, EventArgs e)
{
log4net.Config.XmlConfigurator.Configure();
// Code that runs on application startup
log.Info("Debug: Starting Application");
log.Debug("Debug: Starting Appplication");
}
however the above messages are not being outputted to either output window or the text file??
Am I doing something wrong here? (there are no errors being thrown either).
ASKER
ok I have managed to get a bit further now.
There seemed to be an issue with the file location (probably the ~)
changing this to
is now creating the log file in the root of the web application.
However, the initial .Info, .Debug and .Error calls are not adding to the file? BUT an error is being thrown in the application and the 'This is my error' log is being added to the file.
any ideas why this is happening? as I would like to add debug message through the application to track progress?
There seemed to be an issue with the file location (probably the ~)
<file value="~/Logs/errorlogfile.txt" />
changing this to
<file value="errorlogfile.txt" />
is now creating the log file in the root of the web application.
However, the initial .Info, .Debug and .Error calls are not adding to the file? BUT an error is being thrown in the application and the 'This is my error' log is being added to the file.
any ideas why this is happening? as I would like to add debug message through the application to track progress?
private static readonly log4net.ILog log = log4net.LogManager.GetLogger
(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
void Application_Start(object sender, EventArgs e)
{
log4net.Config.XmlConfigurator.Configure();
// Code that runs on application startup
log.Info("Debug: Starting Application");
log.Debug("Debug: Starting Application");
log.Error("Debug: Starting Application");
}
void Application_End(object sender, EventArgs e)
{
// Code that runs on application shutdown
}
void Application_Error(object sender, EventArgs e)
{
// Code that runs when an unhandled error occurs
log.Error("This is my error", Server.GetLastError());
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Kaufmed,
first of all many thanks for your time on this. It was indeed the filters that were preventing the logging.
It appears the filters are case sensitive, so whilst I was ftrying to catch the filter 'debug' in the web.config I was sending 'Debug' in themessage and so it was ignoring it.
Many thanks for all your time and help.
One last question. Is it possible and where would I add a global
private static readonly log4net.ILog log = log4net.LogManager.GetLogg er
(System.Reflection.MethodB ase.GetCur rentMethod ().Declari ngType);
so that i could just call log.info, log.error from any .cs page?
first of all many thanks for your time on this. It was indeed the filters that were preventing the logging.
It appears the filters are case sensitive, so whilst I was ftrying to catch the filter 'debug' in the web.config I was sending 'Debug' in themessage and so it was ignoring it.
Many thanks for all your time and help.
One last question. Is it possible and where would I add a global
private static readonly log4net.ILog log = log4net.LogManager.GetLogg
(System.Reflection.MethodB
so that i could just call log.info, log.error from any .cs page?
Well, you made it static, so if you also make it public it will be accessible from any page. The only caveat is that you have to prefix the variable name with the containing class name. It probably would not make sense to leave it within any particular page, so I would suggest moving it to Global.asax. Then you simply would call it as:
...because Global is the class defined within Global.asax. If you don't have a Global.asax, simply add a new item to your project. It will be called "Global application class" in the list of items.
Global.log.Error("some error");
...because Global is the class defined within Global.asax. If you don't have a Global.asax, simply add a new item to your project. It will be called "Global application class" in the list of items.
ASKER
thank you for your excellent support.
Open in new window
This needs to be included outside of any namespace. The AssemblyInfo.cs file might be a good place to put it.
Also, I don't know if it was just a copy/paste error, but your variable declaration doesn't match what the tutorial says. You are missing a bit:
Open in new window