gpt4 book ai didi

c# - Nlog 使用 DI 和 appsettings.json 记录到数据库 .NET 5.0

转载 作者:行者123 更新时间:2023-12-05 01:55:59 26 4
gpt4 key购买 nike

我一直在尝试让 Nlog 将日志记录发送到数据库,但我发现的所有示例都返回内部 nlog 错误。我在 .NET 5.0 的控制台应用程序中使用依赖注入(inject)...:

Info Adding target ConsoleTarget(Name=console)
Warn Error has been raised. Exception: NLog.NLogConfigurationException: Failed to create Target of type: Database
---> System.ArgumentException: Target cannot be found: 'Database'. Extension NLog.Database not included?
at NLog.Config.Factory`2.CreateInstance(String itemName)
at NLog.Config.LoggingConfigurationParser.FactoryCreateInstance[T](String classType, INamedItemFactory`2 factory)
--- End of inner exception stack trace ---
Warn Error has been raised. Exception: NLog.NLogConfigurationException: Target 'dbTarget' not found for logging rule: *.

在我的 json 中,我有以下内容:

  "NLog": {
"internalLogLevel": "info",
"internalLogFile": "${basedir}/logs/internal-nlog.txt",
"extensions": {
"NLog.Extensions.Logging": {
"assembly": "NLog.Web.AspNetCore"
}
},
"targets": {
"database": {
"type": "Database",
"name": "dbTarget",
"dbProvider": "Microsoft.Data.SqlClient.SqlConnection,Microsoft.Data.SqlClient",
"connectionString": "myconnectionstring",
"commandText": "INSERT INTO dbo.Logs (Level,CallSite,Type,Message,StackTrace,InnerException,AdditionalInfo) values (@level, @callSite, @type, @message, @stackTrace, @innerException, @additionalInfo)",
"parameters": [
{
"lots of parameters follow"
}
]

}
},
"rules": [
{
"logger": "*",
"minLevel": "Debug",
"writeTo": "dbTarget"
}
]

这是在我的 Main() 中:

        static void Main(string[] args)
{
ApplicationSettings applicationSettings = new ApplicationSettings();

var settings = applicationSettings.GetConfiguration();

var host = Host.CreateDefaultBuilder()
.ConfigureServices((context, services) =>
{
services.AddSingleton<IApplicationSettings, ApplicationSettings>();
services.AddSingleton<IApplication, Application>();
services.AddSingleton<ITeamMemberProcessor, TeamMemberProcessor>();
services.AddLogging(loggingBuilder =>
{
loggingBuilder.ClearProviders();
loggingBuilder.AddNLog();
});
})
.Build();


var svc = ActivatorUtilities.CreateInstance<Application>(host.Services);
svc.Run();
}

我错过了什么吗?写入文件和写入控制台工作正常。另外,据我了解,不应再使用 nlog.config。我们应该使用 appsettings.json 吗?

谢谢!

戴夫

最佳答案

我猜您正在使用 NLog 5.0,其中 NLog DatabaseTarget 为 extracted into a seperate nuget-package .

猜猜错误消息想说什么:

Exception: NLog.NLogConfigurationException: Failed to create Target of type: Database
---> System.ArgumentException: Target cannot be found: 'Database'. Extension NLog.Database not included?

它要你安装扩展 NLog.Database来自 nuget,并将其添加到 NLog 配置中。像这样:

"NLog": {
"internalLogLevel": "info",
"internalLogFile": "${basedir}/logs/internal-nlog.txt",
"extensions": [
{ "assembly": "NLog.Database" },
{ "assembly": "NLog.Web.AspNetCore" }
],
"targets": {
"database": {
"type": "Database",
"name": "dbTarget",
"dbProvider": "Microsoft.Data.SqlClient.SqlConnection,Microsoft.Data.SqlClient",
"connectionString": "myconnectionstring",
"commandText": "INSERT INTO dbo.Logs (Level,CallSite,Type,Message,StackTrace,InnerException,AdditionalInfo) values (@level, @callSite, @type, @message, @stackTrace, @innerException, @additionalInfo)",
"parameters": [
{
"lots of parameters follow"
}
]

}
},
"rules": [
{
"logger": "*",
"minLevel": "Debug",
"writeTo": "dbTarget"
}
]
}

关于c# - Nlog 使用 DI 和 appsettings.json 记录到数据库 .NET 5.0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69975487/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com