0

enter image description here 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());

    }
}
6
  • 2
    EventLog.CreateEventSource(logName, logName); needs administrative rights... But you could post your error to get us on the right track Commented Jan 22, 2013 at 14:12
  • BTW. i think NLog supports EventLog as Target. Commented Jan 22, 2013 at 14:13
  • I have all permissions and I am looged in as administrator only still I am getting the same error Commented Jan 22, 2013 at 14:15
  • 1
    Is your web application running as Administrator, too? Commented Jan 22, 2013 at 14:20
  • The way Windows UAC works is you might be logged in as administrator, but by default when you start a program, it runs as a non-admin user unless the program requests admin rights. Commented Jan 22, 2013 at 14:52

1 Answer 1

1

I ran the below cmd (with administrative rights) on my machine and I can write to application eventlog now

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

Thanks for the suggestions and comments.

Sign up to request clarification or add additional context in comments.

1 Comment

This command creates the log and insert a first information line. After that it isn't needed anymore. People who is releasing installation packages should put something similar in their scripts.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.