Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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");
        }
    }
like image 960
heyou Avatar asked Jul 02 '16 17:07

heyou


People also ask

How do you write an event log?

The Write-EventLog cmdlet writes an event to an event log. To write an event to an event log, the event log must exist on the computer and the source must be registered for the event log. The cmdlets that contain the EventLog noun (the EventLog cmdlets) work only on classic event logs.

How do you write log in Azure?

Enable application logging (Windows) To enable application logging for Windows apps in the Azure portal, navigate to your app and select App Service logs. Select On for either Application Logging (Filesystem) or Application Logging (Blob), or both.

How do I add logs to Event Viewer?

Open "Event Viewer" by clicking the "Start" button. Click "Control Panel" > "System and Security" > "Administrative Tools", and then double-click "Event Viewer" Click to expand "Windows Logs" in the left pane, and then select "Application". Click the "Action" menu and select "Save All Events As".


2 Answers

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.

like image 67
sirdank Avatar answered Nov 09 '22 23:11

sirdank


I know this is an old topic, but this may help someone searching around as I was...

You can use the Microsoft.Extensions.Logging.ILogger object to log out. Then in Azure, if you go to your App Service, left-hand menu, "Diagnose and solve problems" --> Diagnostic Tools (in the main pane) --> Support Tools/Application Event Logs on the left-hand menu of the new screen.

You should see your events output in that log stream!

I should note that this won't be the complete detailed log, but will be the Errors. For the complete logging solution, connect your app to Application Insights as previously suggested. However, for a quick and easy solution, to see error output, this is quite handy.

like image 40
Yusuf Bhayat Avatar answered Nov 09 '22 23:11

Yusuf Bhayat