System.Diagnotics.EvengLog does not work
It works if you add in the .csproj file :
<ItemGroup>
<FrameworkReference Include="Microsoft.WindowsDesktop.App" />
</ItemGroup>
(tested with .NET 5 Console, on Windows 10 1909)
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
I have a .Net 5 console application and want to write messages to Windows EventLog.
System.Diagnotics.EvengLog does not work also can't add Microsoft.Extensions.Logging.EventLog as getting error as it is targeted to .net framework 4.5.1
Why is it so difficult? Can't find anything useful so far!!!
Any help would be much appreciated.
Thanks.
System.Diagnotics.EvengLog does not work
It works if you add in the .csproj file :
<ItemGroup>
<FrameworkReference Include="Microsoft.WindowsDesktop.App" />
</ItemGroup>
(tested with .NET 5 Console, on Windows 10 1909)
Windows Event Log is a Windows-specific function, but .Net 5 is cross-platform. If you run this code on Linux, there will be problems.
The solution is simple, just add a judgment statement:
static void Main(string[] args)
{
if (OperatingSystem.IsWindows())
{
using (EventLog eventLog = new EventLog("Application"))
{
eventLog.Source = "Application";
eventLog.WriteEntry("Log message example", EventLogEntryType.Information, 101, 1);
}
}
}
If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
You have to install the Microsoft.Extensions.Logging.EventLog NuGet package first, to be able to use the EventLog class.