2

I'm trying to build a .NET Core WorkerService that runs as a Windows Service and is able to log to the Application Event Log.

I'm not getting any errors or anything the service does run using the sc create then sc start but I'm getting nothing in the event application event log as expected.

What have I missed?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.EventLog;

namespace FreshIQPrinterProxy
{
    public class Program
    {
        public static void Main(string[] args)
        {
            CreateHostBuilder(args).Build().Run();
        }

        public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
            .UseWindowsService()
            .ConfigureLogging((context, logging) =>
            {
                logging.AddEventLog(new EventLogSettings()
                {
                    SourceName = "MyApp"
                });
            })
            .ConfigureServices((hostContext, services) =>
            {
                services.AddHostedService<Worker>();
            });
    }
}

Here is my Worker.cs file

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.EventLog;

namespace FreshIQPrinterProxy
{
    public class Worker : BackgroundService
    {
        private readonly ILogger<Worker> _logger;

        public Worker(ILogger<Worker> logger)
        {
            _logger = logger;
        }

        protected override async Task ExecuteAsync(CancellationToken stoppingToken)
        {
            while (!stoppingToken.IsCancellationRequested)
            {
                _logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now);
                await Task.Delay(1000, stoppingToken);
            }
        }
    }
}

And here is my appsettings.json file

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },
  "EventLog": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  }
}
Chris Ward
  • 771
  • 1
  • 9
  • 23

1 Answers1

8

In your appsettings.json, the EventLog node needs to be underneath the Logging node, e.g.:

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    },
    "EventLog": {
      "LogLevel": {
        "Default": "Information",
        "Microsoft.Hosting.Lifetime": "Information"
      }
    }
  }
}
Stephen Cleary
  • 437,863
  • 77
  • 675
  • 810
  • Awesome I knew it was something stupidly simple that I was missing. One other question maybe you know, when I run from within Visual Studio the Source is not getting set to what I have "MyApp" but when I run as a Windows Service it is. Can anything be done with that would be helpful for debugging during development for the source to be set correctly so I could filter by that. – Chris Ward Mar 27 '20 at 11:51
  • @ChrisWard: I'm not sure what would cause that. I recommend asking a separate question. – Stephen Cleary Aug 14 '23 at 11:25
  • thanks for the reply but that was 3+ years ago :) – Chris Ward Aug 24 '23 at 15:07
  • Yeah, sorry for the late ping. I happened to look up this question recently and realized I never responded! – Stephen Cleary Aug 24 '23 at 16:20