2013-01-22 46 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

相關問題