2013-01-22 47 views
0

enter image description here我想写我的网站例外到应用程序事件日志,我尝试了不同的方式,我看到这么多的帖子,但我仍然无法做到这一点。请任何人都帮我试图做到这一点。这里是我的代码写入应用程序事件日志

public class CustomHandleErrorAttribute : ActionFilterAttribute 

{ 
    public override void OnActionExecuted(ActionExecutedContext filterContext) 
    { 
     { 
      // Bail if we can't do anything; app will crash. 
      if (filterContext == null) 
       return; 
      // since we're handling this, log to ELMAH(Error logging modules and handler) 

      var ex = filterContext.Exception ?? new Exception("No further information exists."); 
      WriteToEventLog(ex); 

      filterContext.ExceptionHandled = true; 
      var data = new ErrorPresentation 
      { 
       ErrorMessage = HttpUtility.HtmlEncode(ex.Message), 
       TheException = ex, 
       ShowMessage = filterContext.Exception != null, 
       ShowLink = false 
      }; 

      filterContext.Result = new ViewResult 
             { 
              ViewName = "~/Views/Home/ErrorPage.aspx" 
             }; 
     } 
    } 

    public void WriteToEventLog(Exception exception) 
    { 
     // todo : now I need to write this exception to Event log 

     String cs = "FailureAudit"; 
     var thisMachineName = "abc"; 
     var logName = "Application"; 

     EventLog eventLog = new EventLog(logName, thisMachineName); 
     if (!EventLog.SourceExists(logName)) 
     { 
      EventLog.CreateEventSource(logName, logName); 

     } 
     eventLog.Source = cs; 
     eventLog.EnableRaisingEvents = true; 
     //eventLog.WriteEntry(exception.ToString(), EventLogEntryType.Error); 
     eventLog.WriteEntry(exception.ToString()); 

    } 
} 
+2

'EventLog.CreateEventSource(logName,logName);'需要管理权限... 但是你可以发布你的错误,让我们在正确的轨道 – TGlatzer

+0

顺便说一句。我认为NLog支持EventLog作为目标。 – TGlatzer

+0

我拥有所有的权限,而且我只是以管理员身份进入,但仍然出现同样的错误 – 62071072SP

回答

1

我在我的机器上运行下面的命令行(具有管理权限),我可以编写应用程序事件日志现在

eventcreate /ID 1 /L APPLICATION /T INFORMATION /SO MYSOURCE /D "MY-SOURCE" 

感谢您的建议和意见。

+0

该命令创建日志并插入第一条信息行。之后就不再需要了。发布安装包的人应该在脚本中加入类似的东西。 – ManuelJE

相关问题