Skip to content

Using ILoggerFactory.AddEventLog() + ILogger.LogInformation() produces Windows Event entries with "the message resource is present but the message is not found in the string/message table" messages | missing EventLogMessages.dll message file #1776

@EchoFoxxtrot

Description

@EchoFoxxtrot

Following documentation found at https://docs.microsoft.com/en-us/aspnet/core/fundamentals/logging/?view=aspnetcore-2.1#log-category, I configured my ASP.NET Core 2.1 MVC app as follows:

public void Configure (IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) {
      if (env.IsDevelopment ()) {
        app.UseDeveloperExceptionPage ();
        app.UseWebpackDevMiddleware (new WebpackDevMiddlewareOptions {
          HotModuleReplacement = true,
          HotModuleReplacementEndpoint = "/dist/__webpack_hmr"
        });
      } else {
        app.UseExceptionHandler ("/Home/Error");
      }
      app.UseAuthentication ();

      app.UseStaticFiles ();

      loggerFactory.AddEventSourceLogger();
      loggerFactory.AddEventLog(new EventLogSettings {
        SourceName = Configuration["Application:Name"],
        Filter = (x, y) => y > LogLevel.Debug
      });
      ApplicationLogging.LoggerFactory = loggerFactory;
      ApplicationLogging.Log = ApplicationLogging.CreateLogger(Configuration["Application:Name"]);
      loggerFactory.AddConsole (Configuration.GetSection ("Logging"));
      loggerFactory.AddDebug ();
      if (!EventLog.SourceExists(Configuration["Application:Name"]))
      {
          EventLog.CreateEventSource(Configuration["Application:Name"], "Application");
      }

      app.UseSession ();

      app.UseMvc (routes => {
        routes.MapRoute (
          name: "default",
          template: "{controller=Home}/{action=Index}/{id?}");
        routes.MapRoute (
          name: "signout",
          template: "{controller=Home}/{action=SignOut}/{id?}");
        // a special route for our index page
        routes.MapSpaFallbackRoute (
          name: "spa-fallback",
          defaults : new { controller = "Home", action = "index" });
      });
    }

Note that it is using a static class to provide direct reference to the ILogger object reference.

I'm then calling the logger as follows from an attribute class that I decorate my controllers with to log all API requests from each user:

    public class LogAttribute : Attribute, IActionFilter
    {
      public LogAttribute() {}

      public void OnActionExecuted(ActionExecutedContext context)
      {
      }

      public void OnActionExecuting(ActionExecutingContext context)
      {
        var ctx = context.HttpContext;
        var sub = (from c in ctx.User.Claims where c.Type == "sub"
                  select c).FirstOrDefault ().Value;

        ApplicationLogging.Log.LogInformation($"User: {sub}\nRequest: {ctx.Request.Path.Value}");
      }
    }

The resulting output in the Windows Event Application log looks like the following:

The description for Event ID 0 from source MyAppName 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:

MyAppName [from Configuration]
User: USERNAME
Request: /api/Controller/Method

the message resource is present but the message is not found in the string/message table

The EventLog.CreateEventSource(Configuration["Application:Name"], "Application"); call creates a registry entry for the application under \HKLM\SYSTEM\CurrentControlSet\services\eventlog\Application\MyAppName as I would expect, however, the EventMessageFile value is referencing "C:\Program Files\dotnet\shared\Microsoft.NETCore.App\2.1.0\EventLogMessages.dll", which is a non-existent DLL in any of the .NET Core versions that I have installed all the way to 2.0.0.

Since other incarnations of the .NET Framework all seem to include a similar EventLogMessages.dll file, I am assuming this reference to be erroneous.

Is there currently a way for System Event logging to be conveyed cleanly without being polluted with the boilerplate "the message resource is present but the message is not found in the string/message table" messages in each event entry? The official documentation does not address this.

Note: One blog I ran accross at http://blog.ablikim.com/post/7-things-worth-knowing-about-asp-net-core-logging suggested injecting ILogger into a controller using ILogger<DebugLogger>, however that just uses the "Microsoft.Extensions.Logging.Debug.DebugLogger" source instead of "MyAppName" and did not help clean up the entries. I have tried substituting existing EventLogMessages.dll files in the registry entry for my application such as the one for .NET Framework 4.0, but I still get the same messages. I even went through the pains of creating my own event messages DLL based on the Microsoft documentation for MC file format and using the technique found at this link: https://www.codeproject.com/Articles/4153/Getting-the-most-out-of-Event-Viewer

This did not help either.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions