My Programm.cs:
var logger = LogManager.Setup()
.RegisterNLogWeb()
.LoadConfigurationFromFile("nlog.config")
.GetCurrentClassLogger();
try
{
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddControllers(options => { options.Filters.Add<LogResponseFilterAttribute>();
});
builder.Services.AddScoped<LogResponseFilterAttribute>();
builder.Logging.ClearProviders();
builder.Logging.SetMinimumLevel(LogLevel.Information);
builder.Host.UseNLog();
var app = builder.Build();
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();
}
catch (Exception e)
{
logger.Error(e, "Stopped program because of exception");
throw;
}
finally
{
LogManager.Shutdown();
}
My nlog.config:
<?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"
internalLogLevel="Error"
throwExceptions="false"
internalLogFile="c:\logs\internal-log.txt">
<variable name="iis_sitename" value="${gdc:item=iis_sitename}"/>
<variable name="logDirectory" value="c:/logs/${iis_sitename}"/>
<variable name="logSourceName" value="dpc"/>
<targets async="true">
<target xsi:type="File"
name="jsonFile"
fileName="${logDirectory}/${logSourceName}-${lowercase:${level}}.jsonl"
archiveFileName="${logDirectory}/archives/${logSourceName}-${lowercase:${level}}_${date:format=yyyy-MM-dd}.jsonl"
archiveAboveSize="1000240"
archiveNumbering="Sequence"
archiveEvery="Day"
concurrentWrites="true"
keepFileOpen="true"
maxArchiveFiles="1"
encoding="UTF-8">
<layout xsi:type="JsonLayout"
includeAllProperties="true"
maxRecursionLimit="2"
suppressSpaces="true">
<attribute name="timestamp" layout="${longdate}"/>
<attribute name="message" layout="${message}"/>
</layout>
</target>
</targets>
<rules>
<logger name="*" minlevel="Info" writeTo="jsonFile" />
</rules>
</nlog>
How do I call it in an action:
[HttpGet("products")]
public Task<object?> Get(CancellationToken cancellationToken, [FromQuery] Filter filter)
{
_logger.LogInformation("Hello world!");
... other stuff
}
And what I get:
And I'm confused! What is this? Can I have just my "Hello world"? All this smart stuff looks very cool and probably may be used for a new season of "Mr. Robot", but I do not need it! I need just my "Hello world"!

A fundamental concept across all logging libraries is that the Log Severity Level is the primary filter criteria when you specify the verbosity of the logs that you wish to capture.
In your
nlog.configthe primary filter for the internal log file is defined by theminlevelattribute, this is the minimal log level to log. In your case you have specifiedInfo, which corresponds to all Information that highlights progress or application lifetime events.This first mechanism you should use to differentiate your logs from standard
Infolog messages is to use_logger.LogWarninginstead ofLogInformationin your code. Think of warning to mean, "more important than the standard Information, but less than an Error". Instead of the dictionary interpretation to mean that something might be wrong or you are close to some sort of error threshold or state.Noticelevel that is higher thanInfobut less thanWarningthat might seem more appropriate but neither MS Logging Extensions nor NLog explicitly support this.So knowing that the framework and other system tools will report lifetime events using
Info, by default in your application logic you should useLogWarning()to easily differentiate your important messages that are neitherCriticalnorErrorThen in your config rules you can specify
Warnas the log verbosity:You can also use Rules that target specific loggers that might be active in your runtime, if you know them. By Convention, internal .Net loggers use the fully qualified class name as the logger name. Knowing this you
Then you can specify a different
minLevelfor loggers that match your name pattern criteria. Since NLog5 we can usefinalMinLevelto simplify the syntax for combining logging rules, the following example will only logWarnor higher for common System and Microsoft framework loggers, but still allowInfomessages from our application runtime to be captured in the log output.