简体   繁体   English

写入应用程序事件日志

[英]Writing to the application event log

在此处输入图片说明 I am trying to write my website exceptions to the application event log and I tried different ways and I saw so many postings but still I am not able to do that. 我正在尝试将我的网站异常写入应用程序事件日志,并且尝试了不同的方式,并且看到了很多帖子,但是仍然无法做到这一点。 Please anyone help me in trying to do this. 请任何人帮助我尝试这样做。 Here is my code 这是我的代码

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());

    }
}

I ran the below cmd (with administrative rights) on my machine and I can write to application eventlog now 我在计算机上运行了以下cmd(具有管理权限),现在可以写入应用程序事件日志

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

Thanks for the suggestions and comments. 感谢您的建议和意见。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM