Exception catching and log4net usage

catch exception

1. There is an event in the golbal of webform to capture exceptions
2. The filter (included in asp.net mvc) is used in asp.net mvc to capture exceptions. The specific methods are as follows
2.1 Customize the exception capture filter, make it inherit HandleErrorAttribute (exception filter in filter), and override the OnException method
public class MyHandleErrorAttribute:HandleErrorAttribute
{
public override void OnException(ExceptionContext filterContext)
{
base.OnException(filterContext);


//Exception handling records, that is, writing logs, concurrent (multiple threads write things to a file at the same time, an error will be reported) processing is implemented in LogHelper       
LogHelper.WriteLog(filterContext.Exception.StackTrace);


// jump to the error page
filterContext.HttpContext.Response.Redirect("~/Error.html");
}
}
2.2 Registering custom filters
public class FilterConfig
{
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
//filters.Add(new HandleErrorAttribute());
filters.Add(new MyHandleErrorAttribute());
}
}
Find the method of this class: find FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters) in the Application_Start method in Global, right-click to go to the definition

log4net usage

1. Introduce dll
2. Configure in config file
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
</configSections>
</configuration>
<log4net>
    <!-- OFF, FATAL, ERROR, WARN, INFO, DEBUG, ALL -->
    <!-- Set root logger level to ERROR and its appenders -->
    <root>
      <level value="ERROR"/>
      <appender-ref ref="SysAppender"/>
    </root>


    <!-- Print only messages of level DEBUG or above in the packages -->
    <logger name="WebLogger">
      <level value="ERROR"/>
    </logger>

<!-- 该appender节意思为以文本的方式记录日志文件 -->
    <appender name="SysAppender" type="log4net.Appender.RollingFileAppender,log4net" >
      <param name="File" value="App_Data/" />
      <param name="AppendToFile" value="true" />
      <param name="RollingStyle" value="Date" />
      <param name="DatePattern" value=""Logs_"yyyyMMdd".txt"" />
      <param name="StaticLogFileName" value="false" />
      <layout type="log4net.Layout.PatternLayout,log4net">
        <param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n"/>
        <param name="Header" value=" ----------------------header-------------------------- " />
        <param name="Footer" value=" ----------------------footer--------- ----------------- " />
      </layout>
    </appender>
    <appender name="consoleApp" type="log4net.Appender. ConsoleAppender,log4net">
      <layout type="log4net.Layout.PatternLayout,log4net">
        <param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" />
      </ layout>
    </appender>
  </log4net>
3. Initialize
  Add the following code in the Application_Start method of the global file
  log4net.Config.XmlConfigurator.Configure();//Read the configuration information about Log4Net in the configuration file.
4. Write logs
  are added where you need to write logs (concurrency issues need to be dealt with, see LogHelper.cs for details)
  ILog log = LogManager.GetLogger("MyLog");//MyLog is just the name of the log

  log.Error(filterContext);//ex.ToString()


LogHelper.csDownload

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325662918&siteId=291194637