Separate query log and exceptions

I have following config

if (Env.IsDevelopment())
{
	SetConfig(new HostConfig
	{
		AddRedirectParamsToQueryString = true,
		DebugMode = true
	});
}
else
{

	SetConfig(new HostConfig
	{
		AddRedirectParamsToQueryString = true,
		DebugMode = false
	});
}

I am using Nlog logging. On production server my log files are logging every SQL query. This is making it difficult to monitor exceptions.

Should queries be logged when debug is set to false?

In my systemd service file I have set Environment=ASPNETCORE_ENVIRONMENT=Production for my production server.

Is it possible to log queries to a separate file or disable it and just log exceptions?

I am using the nlog config from nlog docs:

<targets>
    <!-- write logs to file  -->
    <target xsi:type="File" name="allfile" fileName="${currentdir}/Logs/nlog-all-${shortdate}.log"
            layout="${longdate}|${event-properties:item=EventId_Id}|${uppercase:${level}}|${logger}|${message} ${exception:format=tostring}" />

    <!-- another file log, only own logs. Uses some ASP.NET core renderers -->
    <target xsi:type="File" name="ownFile-web" fileName="${currentdir}/Logs/nlog-own-${shortdate}.log"
            layout="${longdate}|${event-properties:item=EventId_Id}|${uppercase:${level}}|${logger}|${message} ${exception:format=tostring}|url: ${aspnet-request-url}|action: ${aspnet-mvc-action}" />
</targets>

And inside my catch blocks I am using Log.Error(). Is there any way to configure it so I have log files for different parts of my system so I can say log email errors to one file and import errors to a different file?

SQL Queries are logged using the debug log level, i.e. log.Debug(). Neither the DEBUG log level or DebugMode should be enabled in production unless it’s needed to help diagnose an issue in production.

This is my appsettings

"Logging": {
    "IncludeScopes": false,
    "Debug": {
        "LogLevel": {
            "Default": "Warning"
        }
    },
    "Console": {
        "LogLevel": {
            "Default": "Warning"
        }
    }
},

Where should I set it not to log queries?

Also from my post

Is there any way to configure it so I have log files for different parts of my system so I can say log email errors to one file and import errors to a different file?

Can I select different files to log to when logging from code?

NLog’s configuration lists example rules showing that you can create different loggers based on namespace and log levels although all configuration I’m seeing uses XML. But I’m not familiar with NLog configuration, please ask any NLog questions on the NLog project or StackOverflow.

To avoid logging OrmLite Debug SQL you should set the minLevel to the Warn log level which IMO is the most appropriate level for production.

1 Like