将托管 asp 核心记录为 Windows 服务时出错

时间:2021-03-08 16:59:56

标签: c# .net asp.net-core logging

我正在托管一个作为 Windows 服务发布在单个文件中的 asp.core 应用程序 .net 5.0。我添加了这是我的 program.cs

public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
            .ConfigureLogging(logger =>
            {
                logger.AddConsole();
                logger.AddEventSourceLogger();
                logger.AddLog4Net("log4net.config", true);
            })
            .ConfigureWebHostDefaults(webBuilder =>
            {
                webBuilder.UseStartup<Startup>();
            })
        .UseWindowsService();

但是当我在控制台中或作为具有管理员帐户的服务运行它时它工作正常但是当我作为具有本地系统的服务运行它时我收到以下错误,我猜它无法在 EventLog 中创建条目,那么我该如何解决这个问题?

<块引用>

2021-03-08 11:36:14,917 [16] 错误 Microsoft.AspNetCore.Server.Kestrel - 处理 0HM72A139U7NB 时未处理的异常。 System.AggregateException:写入记录器时出错。 (source 'server' 没有在 log 'Application' 中注册。(它在 log 'System' 中注册。) " Source 和 Log 属性必须匹配,或者你可以将 Log 设置为空字符串,它会自动与 Source 属性匹配。NoAccountInfo=无法获取帐户信息。) ---> System.ArgumentException: 源“服务器”未在日志“应用程序”中注册。 (它在日志'System'中注册。)" Source和Log属性必须匹配,或者你可以将Log设置为空字符串,它会自动匹配到Source属性。NoAccountInfo=无法获取帐户信息。 在 System.Diagnostics.EventLogInternal.VerifyAndCreateSource(String sourceName, String currentMachineName) 在 System.Diagnostics.EventLogInternal.WriteEvent(EventInstance 实例,Byte[] 数据,Object[] 值) 在 System.Diagnostics.EventLog.WriteEvent(EventInstance instance, Object[] values) 在 Microsoft.Extensions.Logging.EventLog.WindowsEventLog.WriteEntry(字符串消息,EventLogEntryType 类型,Int32 eventID,Int16 类别) 在 Microsoft.Extensions.Logging.EventLog.EventLogLogger.WriteMessage(String message, EventLogEntryType eventLogEntryType, Int32 eventId) 在 Microsoft.Extensions.Logging.EventLog.EventLogLogger.Log[TState](LogLevel logLevel, EventId eventId, TState state, Exception exception, Func3 formatter) at Microsoft.Extensions.Logging.Logger.<Log>g__LoggerLog|12_0[TState](LogLevel logLevel, EventId eventId, ILogger logger, Exception exception, Func3 formatter, List1& exceptions, TState& state) --- End of inner exception stack trace --- at Microsoft.Extensions.Logging.Logger.ThrowLoggingError(List1 exceptions) 在 Microsoft.Extensions.Logging.Logger.Log[TState](LogLevel logLevel, EventId eventId, TState state, Exception exception, Func3 formatter) at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Infrastructure.KestrelTrace.Log[TState](LogLevel logLevel, EventId eventId, TState state, Exception exception, Func3 formatter) 在 Microsoft.Extensions.Logging.LoggerExtensions.Log(ILogger logger, LogLevel logLevel, EventId eventId, Exception exception, String message, Object[] args) 在 Microsoft.Extensions.Logging.LoggerExtensions.LogCritical(ILogger 记录器,EventId eventId,异常异常,字符串消息,Object[] args) 在 Microsoft.AspNetCore.Server.Kestrel.Core.Internal.HttpConnection.ProcessRequestsAsync[TContext](IHttpApplication1 httpApplication) at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Infrastructure.KestrelConnection1.ExecuteAsync()

0 个答案:

没有答案