C# log4net 记录所有未处理的应用程序错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1367967/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
log4net log all unhandled application errors
提问by kaivalya
Can you point me to some tutorial or samples on how I can log all un-handled exceptionsthat are occurring on my mvc web app using log4net. Thank you
您能否指出一些有关如何使用 log4net 记录在我的 mvc Web 应用程序上发生的所有未处理异常的教程或示例。谢谢
采纳答案by kaivalya
I paste below the code I use on my global.asax that seems to satisfy me for now.. I am getting all unhandled exceptions on my log4net generated log files..
我粘贴在我在 global.asax 上使用的代码下面,它现在似乎让我满意。
public class MvcApplication : System.Web.HttpApplication
{
private static readonly ILog log = LogManager.GetLogger(typeof(MvcApplication));
void Application_Error(Object sender, EventArgs e)
{
Exception ex = Server.GetLastError().GetBaseException();
log.Error("App_Error", ex);
}
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = "" }
);
}
protected void Application_Start()
{
RegisterRoutes(RouteTable.Routes);
log4net.Config.XmlConfigurator.Configure();
}
}
回答by Stuart Grassie
Without being facetious here you go.
不用开玩笑了,你去吧。
try
{
NotImplementedException nie = new NotImplementedException();
throw nie;
}
catch (Exception e)
{
Log.Fatal(e);
}
Assuming you are using .net 3.5, you could use extension methods, and extend System.Exception and add a Log(log4net.ILog log) method, then wherever you catch an exception in your app, assuming you've setup the Log instance correctly, then you easily do it. Your extension class could grow quite a bit, with various overloads for Debug, Info, Warn, Error and Fatal etc.
假设您使用的是 .net 3.5,您可以使用扩展方法,并扩展 System.Exception 并添加一个 Log(log4net.ILog log) 方法,然后您在应用程序中的任何地方捕获异常,假设您已正确设置 Log 实例,那么你很容易做到。您的扩展类可能会增长很多,其中包含 Debug、Info、Warn、Error 和 Fatal 等各种重载。
回答by Arnis Lapsa
You might be interested to check out what Aspect Oriented Programming(AOP) is.
您可能有兴趣了解什么是面向方面编程(AOP)。
We accomplished this with Post sharp(only - we didn't use log4net but custom tracer).
我们使用Post Sharp实现了这一点(仅 - 我们没有使用 log4net,而是使用自定义跟踪器)。
Just check out first if log4net haven't something for this 'out-of-the-box'. I'm not sure about that.
如果 log4net 没有这个“开箱即用”的东西,请先检查一下。对此我不确定。
回答by Peter Lillevold
There is no built-in support for this in log4net. You should implement the Application_Error eventin Global.asax and call your log4net logger there. The event will be triggered for all unhandled events in your app.
log4net 中没有对此的内置支持。您应该在 Global.asax 中实现Application_Error 事件并在那里调用您的 log4net 记录器。应用程序中所有未处理的事件都会触发该事件。
回答by eljhonb
For ASP.NET applications you like to use the System.Web.HttpApplication.Error
event which is placed in Global.asax
file.
对于 ASP.NET 应用程序,您喜欢使用System.Web.HttpApplication.Error
放置在Global.asax
文件中的事件。
protected void Application_Error(Object sender, EventArgs e)
{
Exception ex = Server.GetLastError();
// Stop error from displaying on the client browser
Context.ClearError();
logger.Fatal(ex.ToString);
}
watch Managing unhandled exceptions
watch管理未处理的异常