我使用log4net进行日志记录,我有两个appender一个文件和其他eventlog appender。 我已提到here此链接
我的问题是日志是在文件中生成的,而不是在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="<Event><Date>%date</Date><Message>%message</Message><Stack>%exception</Stack></Event>%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事件中
答案 0 :(得分:2)
我认为你没有在代码中正确引用EventLogger
:
ILog eventLogger = log4net.LogManager.GetLogger("EventLogAppender");
应改为:
ILog eventLogger = log4net.LogManager.GetLogger("EventLogger");