asp.net log4net

web.config

  
    ...
    
      
    
  
  
    ...
    
  

log4net.config



  
    
      

LogService

    public static class LogService
    {
        public static readonly ILog logger = null;
        static readonly string path = System.Configuration.ConfigurationManager.AppSettings["log4net"];

        static LogService()
        {
            string fileName = HttpContext.Current.Server.MapPath("~").TrimEnd('\\') + path;
            System.IO.FileInfo fileInfo = new System.IO.FileInfo(fileName);
            log4net.Config.XmlConfigurator.ConfigureAndWatch(fileInfo);
            log4net.Config.XmlConfigurator.Configure();
            logger = LogManager.GetLogger("LogWritter");
        }

        public static void Warn(object message)
        {
            logger.Warn(message);
        }

        public static void Info(string message)
        {
            logger.Info(message);
        }

        public static void Error(string message)
        {
            logger.Info(message);
        }

        public static void Error(Exception exception)
        {
            logger.Error(FlattenException(exception));
        }

        public static string FlattenException(Exception exception)
        {
            var stringBuilder = new StringBuilder();

            while (exception != null)
            {
                stringBuilder.AppendLine(exception.GetType().FullName);
                stringBuilder.AppendLine(exception.Message);
                stringBuilder.AppendLine(exception.StackTrace);

                exception = exception.InnerException;
            }

            return stringBuilder.ToString();
        }
    }

Global.asax

        protected void Application_Error(object sender, EventArgs e)
        {
            HttpServerUtility server = HttpContext.Current.Server;
            // ...  
            // Use HttpContext.Current to get a Web request processing helper  
            Exception exception = server.GetLastError();
            // Log an exception 
            LogService.Error(exception);

#if !DEBUG
            Server.ClearError();
            Response.Redirect("/error.html");
            Response.End();
#endif
        }

ExceptionHandling

    [AttributeUsage(AttributeTargets.All, AllowMultiple = false, Inherited = true)]
    public class ExceptionHandlingAttribute : ExceptionFilterAttribute
    {
        /// 
        /// http://stackoverflow.com/questions/21580861/how-to-return-json-for-errors-outside-the-webapi-pipeline
        /// 
        /// 
        public override void OnException(HttpActionExecutedContext context)
        {
            // Log an exception 
            LogService.Error(context.Exception);

            ErrResponse err;

            if (context.Exception is BllException)
            {
                var bex = context.Exception as BllException;
                err = new ErrResponse(bex.ErrorCode, bex.ErrorMsg);
            }
            else
            {
                err = new ErrResponse(404, context.Exception.Message);
            }

            context.Response = context.Request.CreateResponse(HttpStatusCode.OK, err);

            base.OnException(context);
        }
    }

Global.asax

        protected void Application_Error(object sender, EventArgs e)
        {
            HttpServerUtility server = HttpContext.Current.Server;
            // ...  
            // Use HttpContext.Current to get a Web request processing helper  
            Exception exception = server.GetLastError();
            // Log an exception 
            LogService.Error(exception);

#if !DEBUG
            Server.ClearError();
            Response.Redirect("/error.html");
            Response.End();
#endif
        }

AssemblyInfo.cs

[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", Watch = true)]

你可能感兴趣的:(asp.net,log4net)