Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

You need admin privilege to create an event source, becuase the name has to be unique, and to check whether it is all the registry items need to be checked including security. I doubt you want to giv your IIS account local admin though. Way we do this is (for desktop apps), is our installer creates the event source (as admin), and then our apps just read write from it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top