本文介绍了ASP.NET Core EventLog提供程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用.NET Framework ASP.NET Core 2.0的项目,并且想要实现对Windows事件日志的记录,就像我阅读此处

I have a project using .NET Framework ASP.NET Core 2.0, and want to implement logging to windows event log, like i read here

添加日志提供程序

public class Program
{
        public static void Main(string[] args)
        {
            BuildWebHost(args).Run();
        }

        public static IWebHost BuildWebHost(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
            .ConfigureLogging((hostingContext, logging) =>
            {
                logging.AddConfiguration(hostingContext.Configuration.GetSection("Logging"));
                logging.AddEventSourceLogger();
                logging.AddConsole();
            })
            .UseStartup<Startup>()
            .Build();
    }

控制器

[Route("api/[controller]")]
public class ShortCodeController : Controller
{
        private readonly ILogger _logger;

        public ShortCodeController(ILogger<ShortCodeController> logger)
        {
            _logger = logger;

            _logger.LogInformation("INIT");
        }

        [HttpGet("{letters}/{digits}/{length}")]
        public string Get(bool letters, bool digits, int length)
        {
            _logger.LogError("TEST");

            return "value";
        }
    }

它适用于控制台,我看到了我的日志消息.但是我无法使用事件查看器在事件日志中找到该消息.为什么?

And it works for console, I see my log messages. But i can't find that messages in event log using event viewer. Why?

推荐答案

logging.AddEventSourceLogger()

用于事件跟踪.

对于事件日志,您要使用:

logging.AddEventLog()

这篇关于ASP.NET Core EventLog提供程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-07 04:06