« Nlog » : différence entre les versions
De Banane Atomic
Aller à la navigationAller à la recherche
(→File) |
|||
(6 versions intermédiaires par le même utilisateur non affichées) | |||
Ligne 17 : | Ligne 17 : | ||
"targets": { | "targets": { | ||
"async": true, | "async": true, | ||
" | "console": { | ||
"type": "ColoredConsole", | "type": "ColoredConsole", | ||
"layout": "${level:uppercase=true} - ${logger} - ${message:withexception=true}" | "layout": "${level:uppercase=true} - ${logger} - ${message:withexception=true}" | ||
Ligne 26 : | Ligne 26 : | ||
"logger": "*", | "logger": "*", | ||
"minLevel": "Info", | "minLevel": "Info", | ||
"writeTo": " | "writeTo": "console" | ||
} | } | ||
] | ] | ||
Ligne 117 : | Ligne 117 : | ||
</filebox> | </filebox> | ||
* [https://github.com/NLog/NLog/wiki/FileTarget-Archive-Examples FileTarget Archive Examples] | * [https://github.com/NLog/NLog/wiki/FileTarget-Archive-Examples FileTarget Archive Examples] | ||
= Rules = | |||
<kode lang='json'> | |||
"rules": [ | |||
{ | |||
"logger": "*", | |||
"minLevel": "Info", | |||
"writeTo": "console, file" | |||
}, | |||
{ | |||
"logger": "*", | |||
"levels": "Debug", | |||
"writeTo": "file" | |||
} | |||
] | |||
</kode> | |||
= Filters = | |||
Filters apply the default action except for the matching conditions. | |||
<kode lang='json'> | |||
"rules": [ | |||
{ | |||
"logger": "*", | |||
"minLevel": "Info", | |||
"writeTo": "console, file", | |||
"filterDefaultAction": "Log", | |||
"filters": [ | |||
{ | |||
"type": "when", | |||
"condition": "contains('${message}','Common')", | |||
"action": "Ignore" | |||
} | |||
] | |||
} | |||
] | |||
</kode> | |||
= Levels = | |||
{| class="wikitable wtp" | |||
! Level | |||
! Description | |||
|- | |||
| FATAL || Fatal error messages. After a fatal error, the application usually terminates | |||
|- | |||
| ERROR || Error messages | |||
|- | |||
| WARN || Warnings which don't appear to the user of the application | |||
|- | |||
| INFO || Informational messages | |||
|- | |||
| DEBUG || Less detailed and/or less frequent debugging messages | |||
|- | |||
| TRACE || Very detailed log messages, potentially of a high frequency and volume | |||
|} |
Dernière version du 19 mars 2023 à 13:31
Links
Console project
Nuget packages
- NLog.Extensions.Logging
- Microsoft.Extensions.DependencyInjection
- Microsoft.Extensions.Configuration.Json
Config file
Be sure to have the config file copied to the output folder while building. |
appsettings.json |
{ "NLog": { "throwConfigExceptions": true, "targets": { "async": true, "console": { "type": "ColoredConsole", "layout": "${level:uppercase=true} - ${logger} - ${message:withexception=true}" } }, "rules": [ { "logger": "*", "minLevel": "Info", "writeTo": "console" } ] } } |
Program
Program.cs |
var logger = LogManager.GetCurrentClassLogger(); try { var config = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true) .Build(); // to log in this class only LogManager.Configuration = new NLogLoggingConfiguration(config.GetSection("NLog")); using var servicesProvider = new ServiceCollection() .AddTransient<MyApp>() .AddLogging(loggingBuilder => { // configure Logging with NLog loggingBuilder.ClearProviders(); loggingBuilder.SetMinimumLevel(Microsoft.Extensions.Logging.LogLevel.Trace); loggingBuilder.AddNLog(config); }).BuildServiceProvider(); var myApp = servicesProvider.GetRequiredService<MyApp>(); myApp.Run(); } catch (Exception ex) { // NLog: catch any exception and log it. logger.Error(ex, "Stopped program because of exception"); throw; } finally { // Ensure to flush and stop internal timers/threads before application-exit (Avoid segmentation fault on Linux) LogManager.Shutdown(); } |
Configuration file
Layout
// default layout "layout": "${longdate}|${level:uppercase=true}|${logger}|${message:withexception=true}" |
Target
ColoredConsole
appsettings.json |
"async": true, // use the asynchronous NLog Console Target "console": { "type": "ColoredConsole", "layout": "${level:uppercase=true} - ${logger} - ${message:withexception=true}", "useDefaultRowHighlightingRules": false, "rowHighlightingRules": [ { "condition": "level == LogLevel.Fatal", "foregroundColor": "Blue", "backgroundColor": "White" } ] } |
File
appsettings.json |
"file": { "type": "File", "fileName": "${basedir}/logs/nlog.log", // Global Mutex Locks "keepFileOpen": true, "concurrentWrites": true, // Size-based file archival "archiveFileName": "${basedir}/archives/nlog.{#####}.log", "archiveAboveSize": 10240, "maxArchiveFiles": 4, "archiveNumbering": "Rolling" } |
Rules
"rules": [ { "logger": "*", "minLevel": "Info", "writeTo": "console, file" }, { "logger": "*", "levels": "Debug", "writeTo": "file" } ] |
Filters
Filters apply the default action except for the matching conditions.
"rules": [ { "logger": "*", "minLevel": "Info", "writeTo": "console, file", "filterDefaultAction": "Log", "filters": [ { "type": "when", "condition": "contains('${message}','Common')", "action": "Ignore" } ] } ] |
Levels
Level | Description |
---|---|
FATAL | Fatal error messages. After a fatal error, the application usually terminates |
ERROR | Error messages |
WARN | Warnings which don't appear to the user of the application |
INFO | Informational messages |
DEBUG | Less detailed and/or less frequent debugging messages |
TRACE | Very detailed log messages, potentially of a high frequency and volume |