What's wrong with System.Diagnostics.EventLog?

Fabig, Richard 20 Reputation points
2024-05-17T15:38:29.7866667+00:00

Dear Community,

I run into trouble... Situation:

I created a WorkerService with VisualStudio 2022 based on .NET 6. To log all Informations I use the System.Diagnostics.EventLog Library. If I run the application in the command prompt everything works great. The WorkerService is doing it's job. Also, I configured it on one of my test servers as windows service. If I try to start the same assymbly as windows service the start fails. The Logging then tells the following:

Application: appsrv-job.exe

CoreCLR Version: 6.0.2924.17105

.NET Version: 6.0.29

Description: The process was terminated due to an unhandled exception.

Exception Info: System.IO.FileNotFoundException: Could not load file or assembly 'System.Diagnostics.EventLog, Version=6.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'. The system cannot find the file specified.

File name: 'System.Diagnostics.EventLog, Version=6.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'

at AppSrv.Job.Program.Main(String[] args)

The DLL is located in the same folder and hase the version, needed.

What could cause this Issue? Where could I have a closer look, too?

Regards,

Richard

.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,493 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Jiale Xue - MSFT 39,411 Reputation points Microsoft Vendor
    2024-05-20T03:06:47.4033333+00:00

    Hi @Fabig, Richard , Welcome to Microsoft Q&A,

    Judging from the error message, it is caused by the Windows service not being able to find the System.Diagnostics.EventLog assembly when starting.

    Ensure that all project dependencies are properly deployed to the server. In particular, the System.Diagnostics.EventLog assembly and other assemblies it may depend on. You can ensure that all necessary files are included by setting PublishSingleFile and IncludeAllContentForSelfExtract in your project.

    <PropertyGroup>
       <PublishSingleFile>true</PublishSingleFile>
       <IncludeAllContentForSelfExtract>true</IncludeAllContentForSelfExtract>
    </PropertyGroup>
    

    Make sure that the version of System.Diagnostics.EventLog matches the version in the error message. Explicitly specify the required version in your project file (.csproj).

    <ItemGroup>
       <PackageReference Include="System.Diagnostics.EventLog" Version="6.0.0" />
    </ItemGroup>
    

    Verify that the System.Diagnostics.EventLog.dll file does exist in the deployment directory. If you are using a publish folder, you can manually check if the file exists.

    Make sure the Windows service account has permission to write to the event log. You can specify a user account with appropriate permissions in the service configuration to run the service.

    Ensure all NuGet packages are restored correctly before building and publishing. You can use the Restore NuGet Packages option in Visual Studio.

    Make sure the .NET 6 runtime is properly installed on your test server. You can try reinstalling or updating to the latest version.

    Best Regards,

    Jiale


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". 

    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.

    0 comments No comments