Event viewer log creation error with MVC 4

Sampath picture Sampath · Aug 2, 2013 · Viewed 9.4k times · Source

Action Method

public ActionResult Index()
 {
  ViewBag.Message = "Modify this template to jump-start your ASP.NET MVC application.";
  LogError("lok", "lok2", "lok3");
  return View();
 }

LogError Method

private void LogError(string transactionId, string sAmountPaid, string deviceId)
        {
            // Creating the source, which will generate error
            if (!EventLog.SourceExists("MyApplication"))
            {
                //Creating log, where error will be logged
                EventLog.CreateEventSource("MyApplication", "MyLog");
            }

            // Creating object of EventLog to log error
            var myAppLog = new EventLog { Source = "MyApplication" };

            //Writing error to log
            myAppLog.WriteEntry(transactionId + sAmountPaid + deviceId);
        }

When I run Home page it always shows "Error. An error occurred while processing your request.".I suspect this is due to unable to create log on "Event Viewer".When I run above code on local host it's working fine.But when I publish it into IIS server on Windows Server 2012 it's giving above error.

My Question: Should I enable any kind of permission for create log on Event viewer.If so how to do that ? My IIS is working on Windows Server 2012.

Answer

Itanex picture Itanex · Aug 3, 2013

This has been asked before. The following link is to that discussion.

Writing to an event log in ASP.NET on Windows Server 2008 IIS7

You will need to give the application the privileges to write to the Windows Event Log.

However, it is not recommended because of several reasons.

  1. Does not scale.
  2. Introduces an attack vector to the server itself, that can be exploited if your application is not secured properly.
  3. Application cannot be hosted in a 'shared hosting' host solution and you would have to have access to OS itself to grant these (or a system that allows this like cPanel)

Alternatives to logging into windows event log:

  1. Log into a Database or to Files
  2. In a distributed architecture, or enterprise level system, send the event log entries into a queue system that passes the data to an aggregation system.