[英]log4net log all unhandled application errors
Can you point me to some tutorial or samples on how I can log all un-handled exceptions that are occurring on my mvc web app using log4net. 你能指点我一些关于如何使用log4net记录我的mvc web应用程序上发生的未处理异常的教程或示例。 Thank you 谢谢
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上使用的代码下面,这似乎现在让我满意..我在我的log4net生成的日志文件中得到所有未处理的异常..
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();
}
}
For ASP.NET applications you like to use the System.Web.HttpApplication.Error
event which is placed in Global.asax
file. 对于ASP.NET应用程序,您希望使用放在Global.asax
文件中的System.Web.HttpApplication.Error
事件。
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 管理未处理的异常
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'. 如果log4net没有这个“开箱即用”的东西,请先检查一下。 I'm not sure about that. 对此我不确定。
There is no built-in support for this in log4net. 在log4net中没有对此的内置支持。 You should implement the Application_Error event in Global.asax and call your log4net logger there. 您应该在Global.asax中实现Application_Error事件并在那里调用log4net记录器。 The event will be triggered for all unhandled events in your app. 将针对您应用中的所有未处理事件触发该事件。
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. 假设您正在使用.net 3.5,您可以使用扩展方法,并扩展System.Exception并添加Log(log4net.ILog日志)方法,然后在应用程序中捕获异常的任何地方,假设您已正确设置Log实例,那么你很容易做到。 Your extension class could grow quite a bit, with various overloads for Debug, Info, Warn, Error and Fatal etc. 您的扩展类可能会增长很多,包括Debug,Info,Warn,Error和Fatal等各种重载。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.