Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 写入应用程序事件日志_C# - Fatal编程技术网

C# 写入应用程序事件日志

C# 写入应用程序事件日志,c#,C#,我正在尝试将我的网站异常写入应用程序事件日志,我尝试了不同的方法,看到了很多帖子,但我仍然无法做到这一点。请任何人帮我做这件事。这是我的密码 public class CustomHandleErrorAttribute : ActionFilterAttribute { public override void OnActionExecuted(ActionExecutedContext filterContext) { { // Ba

我正在尝试将我的网站异常写入应用程序事件日志,我尝试了不同的方法,看到了很多帖子,但我仍然无法做到这一点。请任何人帮我做这件事。这是我的密码

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

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

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

谢谢你的建议和评论

EventLog.CreateEventSource(logName,logName)需要管理权限。。。但是你可以发布你的错误,让我们走上正轨。我认为NLog支持将EventLog作为目标。我拥有所有权限,我只是以管理员身份登录,但我仍然收到相同的错误您的web应用程序是否也以管理员身份运行?Windows UAC的工作方式是您可能以管理员身份登录,但默认情况下,当您启动程序时,它以非管理员用户身份运行,除非程序请求管理员权限。此命令创建日志并插入第一行信息。从那以后就不再需要它了。发布安装包的人应该在脚本中加入类似的内容。