dotnet / extensions

This repository contains a suite of libraries that provide facilities commonly needed when creating production-ready applications.
MIT License
2.69k stars 759 forks source link

Worker Service Generates Error writing to EventLog #1406

Closed isaacrlevin closed 5 years ago

isaacrlevin commented 5 years ago

Describe the bug

Creating a Worker Service that logs to EventLog generates the below error

The description for Event ID 0 from source Application cannot be found. Either the component that raises this event is not installed on your local computer or the installation is corrupted. You can install or repair the component on the local computer.

If the event originated on another computer, the display information had to be saved with the event.

The following information was included with the event: 

Microsoft.Hosting.Lifetime
Application is shutting down...

The message resource is present but the message was not found in the message table

The description for Event ID 0 from source Application cannot be found. Either the component that raises this event is not installed on your local computer or the installation is corrupted. You can install or repair the component on the local computer.

If the event originated on another computer, the display information had to be saved with the event.

The following information was included with the event: 

LinuxWorker.Worker
Worker running at: 4/3/2019 10:39:44 AM

The message resource is present but the message was not found in the message table

To Reproduce

.NET Core SDK (reflecting any global.json): Version: 3.0.100-preview3-010431 Commit: d72abce213

Runtime Environment: OS Name: Windows OS Version: 10.0.17763 OS Platform: Windows RID: win10-x64 Base Path: C:\Program Files\dotnet\sdk\3.0.100-preview3-010431\

Host (useful for support): Version: 3.0.0-preview3-27503-5 Commit: 3844df9537

.NET Core SDKs installed: 3.0.100-preview3-010431 [C:\Program Files\dotnet\sdk]

Code Sample image

CSPROJ

<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <TargetFramework>netcoreapp3.0</TargetFramework>
    <OutputType>Exe</OutputType>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.Extensions.Hosting" Version="3.0.0-preview3.19153.1" />
    <PackageReference Include="Microsoft.Extensions.Logging.EventLog" Version="3.0.0-preview3.19153.1" />
  </ItemGroup>
</Project>

Expected behavior

Worker logs to EventLog successfully

Experienced Behavior

Error gets logged to EventView

Tratcher commented 5 years ago

Hasn't Logging.EventLog always done this? I didn't think this was new with generic host or worker template.

Tratcher commented 5 years ago

Yeah, I get the same warning out of a 2.2 web app.

The description for Event ID 2 from source Application cannot be found. Either the component that raises this event is not installed on your local computer or the installation is corrupted. You can install or repair the component on the local computer.

If the event originated on another computer, the display information had to be saved with the event.

The following information was included with the event: 

Microsoft.AspNetCore.Hosting.Internal.WebHost
Request finished in 2.1067ms 200 

The message resource is present but the message was not found in the message table
isaacrlevin commented 5 years ago

@Tratcher I was under the impression this was fixed at some point. If it is expected behavior, we can close

analogrelay commented 5 years ago

Transferring to Extensions for area-logging. It's not a hosting issue. It does look like we won't be able to do much about it though since it appears to be caused by the fact that we log the user-provided Event ID to the Event Log and there is no description registered with Windows.

glennc commented 5 years ago

AFAIK this is the way this has always worked. What we have done in preview4 of the worker is log to a named source if we can. Which means if you create an event log source with the same name as your app then you will get logs without the error. We also automatically add the eventlog logger so you don't need that part of the code. Because we use the application name if your service is running with enough privileges to create sources then it will do it automatically without you needing to do anything else.

I would note that the logs are actually working, you get the log messages, but the error is also there.

I would either close this or add a docs label and make sure we've documented the behavior of worker in how it adds the event log logger and how to create a source for it.

glennc commented 5 years ago

In addition, outside of what we've done for worker, if you configure the eventlog logger to use a specific source name and create that source you will also not get the error.

rynowak commented 5 years ago

Since we want the user to interact with this setting does it make sense to put it in configuration or code in the template as a placeholder?

glennc commented 5 years ago

Originally story was:

  1. App automatically logs to event log using app name as source
  2. If it can't log to app name as a source it falls back to current behavior described here with a message telling folks how to create a source and make it better
  3. We build features for installing a worker as a windows service and as part of doing that create the source so that most people don't ever see 2.

Currently I think we have 1 and 2, but not 3, and are unlikely to get 3 in 3.0. People need to know how to install the worker as a Windows Service and I think we can make this easier by putting the command to create the log source next to the command that creates the service in all our guidance.

I don't want to put code in that logs to a source that will likely not exist and throw whenever someone tries to run it without the right permissions to create a source. In fact I assume most people want to run it without those privileges so guidance for install time when you have admin rights seems the best option.