log4net logging from web app
I have a framework library which would do a lot of things including logging. We are using log4net for logging. This framework has the static logging class LogManager, a static instance开发者_如何学JAVA of ILog (logger) and static methods for logging INFO, WARNING, DEBUG etc.
The static instance logger is getting initialized like this:
public static class LogManager
{
private static ILog Logger = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
static ActivityLogManager()
{
}
public static void LogDebugy(string message, params object[] messageParameters)
{
// Log the message here
}
}
I have my own doubts if this is the right way to do it. I see from here that each class which needs to log, should declare a static instance of ILog
.
Also I see that I would have to specify the Log4net config file in my framework project to log. Thought I read here that the config would get picked up from the executing assembly, but I don't see it happen.
Is there any way that I can have my logging done from my framework class with the configuration getting picked up from my web application (for my web app) or some other business library project (for my business class libraries)?
I would also like to add some custom info into my log (like custom business object ID etc, application pool, ServerName, ClientHostName, ClientBrowser, ClientOS, Client User etc). Is it possible?
I am using AdoNetAppender.
Edit1: Am using log4net.Config.XmlConfigurator(ConfigFile = @"log4net.config", Watch = true)
in my assembly.cs to point to log4net config file.
Edit2: My ILog Logger
is in my framework LogManager Class, so only one instance is generated for my application.
A lot of questions but about the first question, I use this style for the logger:
private static ILog Log = LogManager.GetLogger(typeof(MyClass));
Of course you can name the variable whatever you like, but this is recommended by the log4net team I think, as this results in only one ILog
-instance per instance of your class.
This is more or less what you are doing already so nothing to worry about there. I'm not sure why you are going the long way through the executing method though.
I usually place the config in the Web.config or App.config. I couldn't deduce if you use any of these for config. If you do use them it's simple. You just add a configuration section for log4net and add the loggers, appenders and filters you need.
If you want to log custom data you can either add it into the log-message, e.g:
Log.WarnFormat(
"Reactor malfunction, temperature {0} celsius."
+ "Evacuate sector {1} immediately",
temp,
sector);
Or if the values needed are stored in some object, you can make a "renderer" for that type, which will custom print when the object is handed as a logging message, e.g Log.Warn(myObjWithCustomFormatting)
.
A object renderer must derive from IObjectRenderer
and register it in the configuration.
We use log4net in a number of our webapps and they are fine. There is some special setup that needs to be done if you want to log to the event log from a webapp, and it depends on which version of windows you are using. In short it requires you to grant permission to the running user to write to the event log. Under Win 7, 2008 etc you must grant the whole branch permission, all others its just the specific app-log.
If you want to log to a file, just make sure the file is in the apps directory and it should be fine.
If you do not declare the configuration for the logging in your log framework, but delay it until your app calls it, this will happen by default with the log4net configuration section. If it is loaded earlier then no, the first loaded version will be used, unless you override it in code.
精彩评论