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:logsinternal-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"!
2
Answers
I just added the following rules to my nlog.config:
I use nlog 5+ version.
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.config
the primary filter for the internal log file is defined by theminlevel
attribute, 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
Info
log messages is to use_logger.LogWarning
instead ofLogInformation
in 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.Notice
level that is higher thanInfo
but less thanWarning
that 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 neitherCritical
norError
Then in your config rules you can specify
Warn
as 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
minLevel
for loggers that match your name pattern criteria. Since NLog5 we can usefinalMinLevel
to simplify the syntax for combining logging rules, the following example will only logWarn
or higher for common System and Microsoft framework loggers, but still allowInfo
messages from our application runtime to be captured in the log output.