0

我正在使用 log4net 进行日志记录,我有两个附加程序一个文件和另一个事件日志附加程序。我在这里引用了这个链接

我的问题是日志是在文件中生成的,而不是在 Windows 事件中生成的。

我的代码是:

<log4net>

<logger name="FileLogger">
  <level value="ERROR" />
  <appender-ref ref="RollingLogFileAppender" />
</logger>
<logger name="EventLogger" additivity="False">
  <level value="ALL" />
  <appender-ref ref="EventLogAppender" />
</logger>


<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
  <file value="Logs\logfile.xml"/>
  <appendToFile value="true"/>
  <rollingStyle value="Date"/>
  <datePattern value="yyyyMMdd"/>
  <maxSizeRollBackups value="10"/>
  <maximumFileSize value="5MB"/>
  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="&lt;Event&gt;&lt;Date&gt;%date&lt;/Date&gt;&lt;Message&gt;%message&lt;/Message&gt;&lt;Stack&gt;%exception&lt;/Stack&gt;&lt;/Event&gt;%newline" />
  </layout>
</appender>
<appender name="EventLogAppender" type="log4net.Appender.EventLogAppender">
  <!--<param name="MvcApplication1" value="eventlog" />-->
  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%date [%thread] %-5level %logger - %message%newline" />
  </layout>
</appender></log4net>

代码方面是:

public class LogManager
{
    private static readonly ILog eventLogger = log4net.LogManager.GetLogger("EventLogAppender");
    private static readonly ILog fileLogger = LogManager.GetLogger("FileLogger");

    public void Error(Exception ex)
    {
        eventLogger.Logger.Log(eventLogger.GetType(), Level.Error, ex.Source, ex);
    }
    public void Info(string message)
    {
        LoggingEventData e = new LoggingEventData()
         {
             Level = Level.Fatal,
             Message = message
         };
        eventLogger.Logger.Log(new LoggingEvent(e));
        fileLogger.Logger.Log(new LoggingEvent(e));
    }
}

我将 application_Error 用于全局级别错误

 protected void Application_Error(object sender, EventArgs e)
    {
        var httpContext = ((MvcApplication)sender).Context;
        var currentRouteData = RouteTable.Routes.GetRouteData(new HttpContextWrapper(httpContext));
        var currentController = " ";
        var currentAction = " ";

        if (currentRouteData != null)
        {
            if (currentRouteData.Values["controller"] != null && !String.IsNullOrEmpty(currentRouteData.Values["controller"].ToString()))
            {
                currentController = currentRouteData.Values["controller"].ToString();
            }
            if (currentRouteData.Values["action"] != null && !String.IsNullOrEmpty(currentRouteData.Values["action"].ToString()))
            {
                currentAction = currentRouteData.Values["action"].ToString();
            }
        }

        var ex = Server.GetLastError();
        var controller = new ErrorController();
        var routeData = new RouteData();
        var action = "Index";

        if (ex is HttpException)
        {
            var httpEx = ex as HttpException;

            switch (httpEx.GetHttpCode())
            {
                case 404:
                    action = "NotFound";
                    break;
                case 401:
                    action = " UnAuthorized";
                    break;
                // others if any
                default:
                    action = "Index";
                    break;
            }
        }
        new LogManager().Error(ex);
        httpContext.ClearError();
        httpContext.Response.Clear();
        httpContext.Response.StatusCode = ex is HttpException ? ((HttpException)ex).GetHttpCode() : 500;
        httpContext.Response.TrySkipIisCustomErrors = true;
        routeData.Values["controller"] = "Error";
        routeData.Values["action"] = action;
        controller.ViewData.Model = new HandleErrorInfo(ex, currentController, currentAction);
        ((IController)controller).Execute(new RequestContext(new HttpContextWrapper(httpContext), routeData));
    }

请帮助我错了为什么错误和信息没有记录在windows事件中

4

1 回答 1

2

我认为您没有EventLogger在代码中正确引用:

ILog eventLogger = log4net.LogManager.GetLogger("EventLogAppender");

应改为:

ILog eventLogger = log4net.LogManager.GetLogger("EventLogger");
于 2014-01-02T06:43:42.753 回答