Log

———-ILogger(原生)———-

前置作業-註冊: services.AddLogging(builder => { builder.AddConfiguration(Configuration.GetSection(‘Logging’)) .AddFilter(‘Microsoft’, LogLevel.Warning)//篩選掉Microsoft.* 的Log .AddConsole() .AddDebug(); });

前置作業-appsetting.json

實作: _logger.LogDebug(‘Hi there’); _logger.LogInformation(‘Hi there’); _logger.LogWarning(‘Hi there’); _logger.LogError(‘Hi there’); _logger.LogCritical(‘Hi there’);

備註: 會寫到事件檢視器中

———-NLog 寫實體檔案———-

前置作業-安裝Package: NLog NLog.Extensions.Logging NLog.Web.AspNerCore(.Net Core 開發)

前置作業-建立Config 在網站根目錄新增NLog.config 檔案 內容: <?xml version=’1.0’ encoding=’utf-8’ ?>

前置作業-註冊: services.AddLogging(logging => { //清除原本的 logging provider(更換 Logging Provider) logging.ClearProviders();

//設定 logging 的 minmum level 為 trace
logging.SetMinimumLevel(Microsoft.Extensions.Logging.LogLevel.Trace);

//使用 NLog 作為 logging provider
logging.AddNLog(); });

實作: _logger.LogDebug(‘Hi there’); _logger.LogInformation(‘Hi there’); _logger.LogWarning(‘Hi there’); _logger.LogError(‘Hi there’); _logger.LogCritical(‘Hi there’);

———-Grafana———- UI Server:docker run -d –name grafana -p 3000:3000 -e GF_SECURITY_ADMIN_USER=admin -e GF_SECURITY_ADMIN_PASSWORD=strongpassword -e TZ=Asia/Taipei grafana/grafana

Log Collection:

程式端使用套件:

標籤: dotnet, logging