c# - ASP.Net Core 1 日志记录错误 - 无法找到来自源应用程序的事件 ID xxxx 的描述

标签 c# asp.net-core event-log

我想从 ASP.Net Core 应用程序的 Controller 方法写入 Windows 事件日志。

我遇到的问题是,在我希望写入日志信息的地方,我不断收到错误/信息日志:

The description for Event ID xxxx 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:

My.Fully.Qualified.Namespace.WebApi.Controllers.MyController Error occured in method 'MyMethod'

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

快速说明一下,在使用 .Net Core 之前,我一直使用 resolved a similar error by creating the Event Source using Powershell or Command 或正确配置 Microsoft.Practices.EnterpriseLibrary.Logging Application block 或其他第三方库

我使用的方法是:

安装 Nuget 包:Microsoft.Extensions.Logging.AbstractionsMicrosoft.Extensions.Logging 后,我在 Start Configure 代码块中指定了 EventLog Provider

    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
    {
        loggerFactory.AddConsole(Configuration.GetSection("Logging"));
        loggerFactory
            .AddDebug()
            .AddEventLog();

        //... rest of the code here
    }

对于每个 Controller ,使用 ILogger<T> 方法:

public class MyController : Controller
{
    private readonly IMyRepository _myRepository;
    private readonly ILogger _logger;

    public MyController(IMyRepository myRepository,
        ILogger<MyController> logger)
    {
        _myRepository = myRepository;
        _logger = logger;
    }

    public bool MyMethod(MyRequestObject request)
    {
        try
        {
            var response = privateDoSomethingMethod(request);

            return response.Success;
        }
        catch (Exception ex)
        {
            _logger.LogError(6666,ex, "Error occured when doing stuff.");

            return false;
        }
    }

这是基于official documentation on Logging

我阅读和尝试过的内容:

最佳答案

我无法在 .Net Core 2.0 中使用 loggerFactory.AddEventLog()。由于 Microsoft.Extensions.Logger.EventLog 仅存在于 .net 4.5.1 中,尽管 Nuget 包可用于 2.0.0 here , 所以看起来它需要移植到 Core。

enter image description here

但也许这对你有用。 添加您的 CustomEventIds

 public static class CustomEventIds
{
    public const int Debug = 100;
    public const int Information = 101;
    public const int Warning = 102;
    public const int Error = 103;
    public const int Critical = 104;
}

和:

_logger.LogInformation(CustomEventIds.Debug, "Starting to do something with input: {0}", "test");

关于c# - ASP.Net Core 1 日志记录错误 - 无法找到来自源应用程序的事件 ID xxxx 的描述,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45861604/

相关文章:

c# - 在 C# 项目中引用 exe 文件是一种不好的做法吗

c# - ASP Net Core - 如何进行多级路由继承?

windows - 如何设置 Powershell where-object 来过滤 EventLog

logging - NLog:无法写入事件日志

windows - 一个进程可以向另一个进程在不同用户名下创建的事件发出信号(两个进程在不同用户名下运行)吗?

c# - PowerShell:为什么我能够在一个 session 中加载同一 .NET 程序集的多个版本以及 "bypass Dependency hell"?

c# - 在 Unity 中使用标签访问所有对象的脚本

c# - 检查字符串是否为空的最佳方法是什么?

asp.net-core - 将 SignalR hub 与 Redis 结合使用时如何跨负载均衡器服务器跨越 ConcurrentDictionary

sql-server - 为什么无法将我的 .net 核心容器连接到 SQL Server docker 容器?