简体   繁体   中英

Using log4net with asp.net web forms

I am trying to incorporate log4net into my web application. I have already done so with the newer portion based on .net mvc, but am having trouble incorporating it into the web forms based portion of my app. I have looked around for an example and have not come across one.

In order to narrow my question down, let's assume I know how to configure my web.config file. My questions are:

(1) I have considered placing the actual call to log4net in a "Global.asax" file as well as placing it in my base page (on which all other pages are built). Should I be placing the actual code in either of these places, and if not where should I put the code?

(2) My understanding of the code is that I need to instantiate a log, and then have that log log stuff when I want to it (the specifics of log being taken care of by web.config), but I don't know what that code should actually look like. So, what code should I actually be placing in my file? (example would be appreciated)

Thanks

just place the code where you need it.

to initiate i just use this line in every page i need it...

static Logger log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

so the logger gets the name of the current class including full namespace. i also use the logger in global.asax for example error logging

protected void Application_Error(object sender, EventArgs e)
    {
        StringBuilder sb = new StringBuilder();
        sb.Append("Unhandled error occured in application. Sender: ");
        sb.AppendLine(Request.RawUrl);
        sb.Append("Query: ");
        sb.AppendLine(Request.QueryString.ToString());

        Exception ex = Server.GetLastError().GetBaseException();

        log.Error(sb.ToString(), ex);
        Server.ClearError();

        Response.Redirect("~/Error.aspx");
    }

but i seperate the log config from the web.config. it's easier for me and you don't have to handle so big files. i also think that the application is not restartet, when you change the log config file. if you update the web.config the application is always restartet as far as i know.

to accomplish this you just need to add following to the web.config in add

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

than add this line somewhere in the web.config

<log4net configSource="log.config"/>

and then the file "log.config" has all the listeners configured. but don't forget to copy the file to your production/test environment. otherwise you may get strange error messages.

hth

Placing it in the base page sounds like a good idea, then when instantiating using GetLogger you can pass in the page you're logging for (being as specific as possible about where your log calls are coming from really helps debugging). You'll also need a call to .Configure after instantiating which is what always catches me out (need to import Log4Net.Configure for this)

Place your listener config in web.config and define min/max levels via LevelRangeFilter (DEBUG is lowest, FATAL is highest).

Usage is pretty simple - you just log with the required level, EG: log.Info, log.InfoFormat, log.Error, log.ErrorFormat (the formats just work like String.Format) - anything below the minimum configured logging level will be ignored so you can put as much logging in as you like.

Have you also checked out Elmah? This allows you to track unhandled exceptions. http://code.google.com/p/elmah/

Let me share with my experience of using logger. Usually I use it with IOC container, ie Windsor. So initialization of logger I make in Global.asax file, as it is runned once per app, and berore all requests. This is proper place to initialize logger. As for the place where you should call the log, there isn't any recommendation. You should call where you need. If you are logging some events based on page lifecycle, of course you should use logger in page code behind file. But if you want to trace your components, better extract logics in separate library and use logger within it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM