I've been trying to get Nlog to send logging to a database, but the examples I've found all return an internal nlog error. I'm using dependency inject in a console application with .NET 5.0...:
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: *.
In my json, I have the following:
"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"
}
]
And this is in my 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();
}
Am I missing something? The write to file and write to console work fine. Also, as I understand, the nlog.config shouldn't be used anymore.. we should be using appsettings.json?
Thanks!
Dave