0

How am i supposed to Log to the Windows EventViewer in a WinUI3 App? I was not able to find a working example.

Current Situation:

In every .NET Framework app i used System.Diagnostics.EventLog

to write more or less useful eventLog.WriteEntry(e.Message, EventLogEntryType.Error);

Messages into the 'Application' Log. The WiX Installer takes care of the EventSource creation during the App installation. Now, i tried to do the same with a WINUI3 App. The logging itself is working as expected. However the eventsource creation is not. I was not able to find any information on how to tell visualstudio to create an eventsource during installation, nor am i confident that this is the right way to go.

So, how am i supposed to do logging on winui if i do not want to log to seperate files wehre i do need seperate programs to read them.

I have had a look at the LoggingSession and LoggingChannel solution with samples. But i do not think this kind of log is consumable without extra tools that needs to be downloaded/installed.

1 Answer 1

0

Install the Microsoft.Extensions.Logging NuGet package and use it like this:

Microsoft.Extensions.Logging.EventLog.EventLogLoggerProvider eventLogLoggerProvider = new();
Microsoft.Extensions.Logging.ILogger logger = eventLogLoggerProvider.CreateLogger("EventLogExample");
logger.LogInformation("This is an information message");
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, Andrew. Unfortunately i have a different Problem. I mean, i already do this. The problem is: I do not know how to tell the MSIX package publishing process to create an eventsource when installing. I tried to do this by the app itself: 'if (!EventLog.SourceExists(Assembly.GetEntryAssembly().GetName().Name)) { EventLog.CreateEventSource(new EventSourceCreationData(Assembly.GetEntryAssembly().GetName().Name, "Application")); }' While this works fine in WPF .NET FW or in debug mode, it does not in winui / .net 8 release build.
I think you should open a new question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.