log4net 写入日志到不同的位置

2018-09-18 06:54:10来源:博客园 阅读 ()

新老客户大回馈,云服务器低至5折

某些业务需要根据不同的功能将日志记录到不同的位置,以便于区分。

<?xml version="1.0" encoding="utf-8" ?>
<log4net>
  <appender name="Console" type="log4net.Appender.ConsoleAppender">
    <layout type="log4net.Layout.PatternLayout">
      <!-- Pattern to output the caller's file name and line number -->
      <conversionPattern value="%5level [%thread] (%file:%line) - %date - %message%newline" />
    </layout>
  </appender>

  <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
    <file type="log4net.Util.PatternString" value="Logs\\Common\\%date{yyyy-MM-dd}.log" />
    <appendToFile value="true" />
    <rollingStyle value="Composite" />
    <datePattern value="yyyyMMdd" />
    <maxSizeRollBackups value="10" />
    <maximumFileSize value="1KB" />
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
    </layout>
  </appender>
  <appender name="EventLoggerAppender" type="log4net.Appender.RollingFileAppender">
    <file type="log4net.Util.PatternString" value="Logs\\Events\\%date{yyyy-MM-dd}.log" />
    <appendToFile value="true" />
    <rollingStyle value="Composite" />
    <datePattern value="yyyyMMdd" />
    <maxSizeRollBackups value="10" />
    <maximumFileSize value="1KB" />
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
    </layout>
  </appender>
  <logger name="FileLogger">
    <level value="ALL" />
    <appender-ref ref="RollingLogFileAppender" />
  </logger>

  <logger name="EventLogger">
    <level value="ALL" />
    <appender-ref ref="EventLoggerAppender" />
  </logger>
  <!-- <root> -->
  <!--   <level value="DEBUG" /> -->
  <!--   <appender-ref ref="RollingLogFileAppender" /> -->
  <!--   <appender-ref ref="Console" /> -->
  <!-- </root> -->
</log4net>

日志工具类(这里只是简单的封装):

public static class LogHelper
{
    private static readonly ILog logger = LogManager.GetLogger("FileLogger");
    private static readonly ILog eventLogger = LogManager.GetLogger("EventLogger");
    static LogHelper()
    {
        log4net.Config.XmlConfigurator.Configure(new FileInfo(HttpContext.Current.Server.MapPath("~/log4net.config")));
    }

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

    public static void LogEvent(string errMessage)
    {
        eventLogger.Error(errMessage);
    }
}
public class HomeController : Controller
{
    public ActionResult Index()
    {
        LogHelper.Error("出错啦");
        LogHelper.LogEvent("fafdsafdsaff");
        ViewBag.Title = "Home Page";

        return View();
    }
}

浏览页面后,可以看到日志目录如下:

 

 

参考资料:https://stackoverflow.com/questions/11930381/log4net-multiple-appenders-writing-to-event-viewer

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:MVC5控制器传值的三种方式(ViewData,ViewBag,TempData),刚

下一篇:生成随机字符