I have implemented NLog for .NET Core 3.1 MVC web app and API projects. The nlog.config file for both of them is nearly identical. I've carefuly checked this and the only difference is the database table name and file name that they log to.
I'm successfully suppressing non-essential Microsoft logs from the API project, but am only partially able to do so for the web app. Specifically, Trace and Info logs by Microsoft that deal with making calls to the API using HttpClient appear in the file and database:
You can see that only one of the logs is a log that I actually wrote, and the rest are were automatically logged by Microsoft.
I'm not sure what to try here, but I've looked at the internal logs and saw nothing out of the ordinary.
I want to know how can I suppress the additional Microsoft logs and what's wrong with my current configuration?
NuGet packages I have installed (identical for both apps):

nlog.config of the web app project:
<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
autoReload="true"
throwConfigExceptions="true"
internalLogLevel="Info"
internalLogFile="c:\temp\internal-CNC_WebUI-nlog.txt">
<extensions>
<add assembly="Nlog.Extensions.Logging"/>
<add assembly="NLog.Web.AspNetCore"/>
</extensions>
<targets>
<target xsi:type="File" name="CNC_WebUIFile" fileName="c:\temp\nlog-CNC_WebUIFile-${shortdate}.log"
layout="${aspnet-traceidentifier}|${configsetting:AppSettings.NlogConnection.AppName}|${event-properties:ObjectID}|${date}|${uppercase:${level}}|${message} ${exception:format=tostring}|${logger}|${callsite:filename=false}|${exception:toString}" />
<target name="databaseLogger" xsi:type="Database"
dbProvider="sqlserver"
dbHost="${configsetting:AppSettings.NlogConnection.DbHost}"
dbDatabase="${configsetting:AppSettings.NlogConnection.Database}"
dbUserName="${configsetting:AppSettings.NlogConnection.User}"
dbPassword="${configsetting:AppSettings.NlogConnection.Password}" >
<commandText>
INSERT INTO dbo.CNC_WebUILogs (
CorrelationId, Application, ObjectID, Logged, Level, Message,
Logger, CallSite, Exception
) VALUES (
@CorrelationId, @Application, @ObjectID, @Logged, @Level, @Message,
@Logger, @Callsite, @Exception
);
</commandText>
<parameter name="@correlationId" layout="${aspnet-traceidentifier}" />
<parameter name="@application" layout="${configsetting:AppSettings.NlogConnection.AppName}" />
<parameter name="@ObjectID" layout="${event-properties:ObjectID}" />
<parameter name="@logged" layout="${date}" />
<parameter name="@level" layout="${level}" />
<parameter name="@message" layout="${message}" />
<parameter name="@logger" layout="${logger}" />
<parameter name="@callSite" layout="${callsite:filename=false}" />
<parameter name="@exception" layout="${exception:toString}" />
</target>
</targets>
<rules>
<logger name="Microsoft.*" levels="Warn,Error,Fatal" writeTo="databaseLogger,CNC_WebUIFile"></logger>
<logger name="Microsoft.*" minlevel="Trace" final="true" />
<logger name="*" minlevel="Trace" writeTo="CNC_WebUIFile" />
<logger name="*" minlevel="${configsetting:AppSettings.NlogConnection.LogLevel}" writeTo="databaseLogger" />
</rules>
</nlog>
One of my repos that had their API call activity logged by Microsoft:
public class MfgrRepo : IMfgrRepo
{
private readonly IHttpClientFactory _clientFactory;
public MfgrRepo(IHttpClientFactory clientFactory)
{
_clientFactory = clientFactory;
}
public async Task<List<MfgrDto>> Get()
{
HttpClient client = _clientFactory.CreateClient(HttpClientConfigNames.CNC);
List<MfgrDto> models = new List<MfgrDto>();
try
{
HttpResponseMessage response = await client.GetAsync("api/Mfgrs");
models.AddRange(await response.Content.ReadFromJsonAsync<List<MfgrDto>>());
}
catch (Exception ex)
{
throw ex;
}
return models;
}
}
You can configure the log levels that the M.E.Logging system uses through the appsettings.json file. This also allows you to configure the types per namespace, so you can make the System.Net.Http namespace silent that way:
{
"Logging": {
"LogLevel": {
"System.Net.Http": "Warning"
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With