Search code examples
c#asp.netazureevent-logevent-viewer

Use Event Log (Write in Event Viewer) in Azure


My website, written in ASP.NET and i used EventLog to write logs into the event viewer. It is already been running in the production (OS: Windows Server 2012 R2) and no problems encountered upon logging some errors. I am now planning to migrate the server to Azure - App Services. I wonder if my code in logging the errors would still work after moving to Azure - App Services?? If yes then how do i view the error that have been logged by my website?? I cannot see Event Viewer in the Azure - App Services. If no then what is the simplest and fastest alternate way to replace my code in logging the errors??

Here is my code:

public static void LogEventLog(string message, EventLogEntryType logType)
    {
        string source = AppConfig.ErrorEventViewerSource;

        // Since we can't prevent the app from terminating, log this to the event log. 
        CreateEventSource(source);

        // Create an EventLog instance and assign its source.
        EventLog myLog = new EventLog();
        myLog.Source = source;
        myLog.WriteEntry(message, logType);

    }



    public static void CreateEventSource(string source)
    {
        if (!EventLog.SourceExists(source))
        {
            EventLog.CreateEventSource(source, "Application");
        }
    }

Solution

  • I think the correct solution is to connect your app to application insights using one of the methods described here. For the short term, to get mine working I had to remove the call to CreateEventSource() and write to an existing log since my app doesn't have permission to create a new log on the app service host.