简体   繁体   English

将log4net与asp.net Web表单结合使用

[英]Using log4net with asp.net web forms

I am trying to incorporate log4net into my web application. 我正在尝试将log4net合并到我的Web应用程序中。 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. 我已经使用基于.net mvc的新部分完成了这项工作,但是我很难将其合并到我的应用程序的基于Web表单的部分中。 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. 为了缩小我的问题范围,让我们假设我知道如何配置我的web.config文件。 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). (1)我考虑将实际调用log4net放在“Global.asax”文件中,并将其放在我的基页(构建所有其他页面)上。 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. (2)我对代码的理解是我需要实例化一个日志,然后在我想要的时候记录日志(日志的细节由web.config处理),但我不知道该代码应该是什么样子。 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 我还使用global.asax中的记录器来进行错误记录

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. 但是我从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. 如果你更新web.config,应用程序总是重新启动,据我所知。

to accomplish this you just need to add following to the web.config in add 要完成此任务,您只需要在web.config中添加以下内容即可

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

than add this line somewhere in the web.config 而不是在web.config中的某处添加此行

<log4net configSource="log.config"/>

and then the file "log.config" has all the listeners configured. 然后文件“log.config”配置了所有侦听器。 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). 将它放在基页中听起来是个好主意,然后在使用GetLogger进行实例化时,您可以传入您正在登录的页面(尽可能具体地说明日志调用的来源真的有助于调试)。 You'll also need a call to .Configure after instantiating which is what always catches me out (need to import Log4Net.Configure for this) 在实例化之后你还需要调用.Configure,这总是让我感到厌烦(需要为此导入Log4Net.Configure)

Place your listener config in web.config and define min/max levels via LevelRangeFilter (DEBUG is lowest, FATAL is highest). 将监听器配置放在web.config中并通过LevelRangeFilter定义最小/最大级别(DEBUG最低,FATAL最高)。

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. 用法非常简单 - 您只需使用所需级别进行日志记录,EG:log.Info,log.InfoFormat,log.Error,log.ErrorFormat(格式就像String.Format一样) - 低于最低配置日志级别的任何内容都将是被忽略,因此您可以根据需要添加尽可能多的登录。

Have you also checked out Elmah? 你也检查过艾玛吗? This allows you to track unhandled exceptions. 这允许您跟踪未处理的异常。 http://code.google.com/p/elmah/ http://code.google.com/p/elmah/

Let me share with my experience of using logger. 让我分享一下使用记录器的经验。 Usually I use it with IOC container, ie Windsor. 通常我会将它与IOC容器一起使用,即Windsor。 So initialization of logger I make in Global.asax file, as it is runned once per app, and berore all requests. 因此我在Global.asax文件中初始化logger,因为每个应用程序运行一次,并且所有请求都是berore。 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. 如果您基于页面生命周期记录某些事件,当然您应该在文件后面的页面代码中使用logger。 But if you want to trace your components, better extract logics in separate library and use logger within it. 但是,如果要跟踪组件,最好在单独的库中提取逻辑并在其中使用记录器。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法在ASP.NET Web窗体站点中使用log4net将日志写入文件 - unable to write logs to a file using log4net in an ASP.NET Web Forms Site asp.net核心上的log4net - log4net on asp.net core 确定ASP.NET WEB API中每个API的执行时间和跟踪,并使用C#使用log4net登录到文件 - Identify execution time and Tracing of each API in ASP.NET WEB API and log to files using log4net using C# 如何在不使用所有可用线程的情况下在ASP.NET MVC Web应用程序中与log4net异步记录? - How to log asynchronously with log4net in an ASP.NET MVC Web Application without using up all available threads? 将jqGrid与Asp.Net Web窗体一起使用 - Using jqGrid with Asp.Net Web Forms 如何在ASP.NET Web API 2中使用log4net布局模式有条件地捕获用户名? - How to conditionally capture user name with log4net layout pattern in ASP.NET web api 2? 如何将Autofac和Log4Net与ASP.NET Web API 2应用程序集成 - How to Integrate Autofac and Log4Net with ASP.NET Web API 2 Application 如何使用ASP.NET会话和log4net基于登录的用户创建日志文件 - How to create a log file based on logged in user using ASP.NET session and log4net 使用Log4Net记录ASP.NET和C#应用程序中调用的所有功能 - Using Log4Net to log all the functions called in ASP.NET and C# application 如何在asp.net中使用Log4net记录客户端IP,浏览器名称和用户名? - How to Log Client Ip, Browser Name and User Name using Log4net in asp.net?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM