Use Event Log (Write in Event Viewer) in Azure
Asked Answered
H

2

8

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");
        }
    }
Hindermost answered 2/7, 2016 at 17:23 Comment(1)
I am also new to Azure but as per my understanding you need to push event logs to diagnostics store. Refer this link - azure.microsoft.com/en-in/documentation/articles/…Lactometer
W
4

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.

Weig answered 21/5, 2018 at 13:13 Comment(1)
Agree i already switch to application insights. I now log exception error directly to the application insights. Thanks!Hindermost
T
2

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.

Typist answered 29/10, 2020 at 15:14 Comment(3)
I’m not sure why you’re suggesting to use a SharePoint module in Azure App Service.Camfort
That was a mistake on my part, it should have been Microsoft.Extension... i'll updateTypist
No worries. Hopefully it helps someone in the future.Camfort

© 2022 - 2025 — McMap. All rights reserved.