【问题标题】:How do I enable Event Logging in a .NET Core WorkerService running as a Window Service如何在作为窗口服务运行的 .NET Core WorkerService 中启用事件日志记录
【发布时间】:2020-03-26 23:29:18
【问题描述】:

我正在尝试构建一个作为 Windows 服务运行并能够记录到应用程序事件日志的 .NET Core WorkerService。

我没有收到任何错误或服务使用 sc create then sc start 运行的任何内容,但我在事件应用程序事件日志中没有得到任何预期。

我错过了什么?

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>();
            });
    }
}

这是我的 Worker.cs 文件

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);
            }
        }
    }
}

这是我的 appsettings.json 文件

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },
  "EventLog": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  }
}

【问题讨论】:

    标签: c# .net-core windows-services


    【解决方案1】:

    在您的appsettings.json 中,EventLog 节点需要位于 Logging 节点下方,例如:

    {
      "Logging": {
        "LogLevel": {
          "Default": "Information",
          "Microsoft": "Warning",
          "Microsoft.Hosting.Lifetime": "Information"
        },
        "EventLog": {
          "LogLevel": {
            "Default": "Information",
            "Microsoft.Hosting.Lifetime": "Information"
          }
        }
      }
    }
    

    【讨论】:

    • 太棒了,我知道我错过了一些简单得愚蠢的事情。另一个问题也许你知道,当我在 Visual Studio 中运行时,源并没有设置为我拥有的“MyApp”,但是当我作为 Windows 服务运行时它是。可以做任何事情来帮助在开发过程中进行调试,以便正确设置源代码,以便我可以通过它进行过滤。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-11-18
    • 1970-01-01
    • 2010-12-03
    • 2010-09-13
    • 2019-10-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多