1. 程式人生 > 其它 >.net core winform 使用Nlog

.net core winform 使用Nlog

折騰了好幾次,自己網上找了很多關於log4net、Nlog的,試了很久,才弄好一個Nlog的,後續看下搞定log4net,再加上

呼叫程式碼

var logger =  NLogBuilder.ConfigureNLog("nlog.config").GetCurrentClassLogger();
logger.Error("報錯資訊");

配置檔案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="info" internalLogFile="${basedir}\log\internal-nlog.log"> <!-- enable asp.net core and mongodb layout renderers --> <extensions> <add assembly="NLog.Web.AspNetCore"/> </extensions
> <!--internal-nlog:NLog啟動及載入config資訊--> <!--nlog-all:所有日誌記錄資訊--> <!--nlog-own:自定義日誌記錄資訊--> <!-- the targets to write to --> <targets> <!-- write logs to file --> <target xsi:type="File" name="allfile" fileName="${basedir}\log\nlog-all-${shortdate}.log"
layout="${longdate} >> [日誌級別:${uppercase:${level}}]${newline}日誌來源:${logger}${newline}日誌資訊:${message}${newline}錯誤資訊:${exception:format=tostring}${newline}==============================================================${newline}" /> <target xsi:type="File" name="error" fileName="${basedir}\log\nlog-err-${shortdate}.log" layout="${longdate} >> [日誌級別:${uppercase:${level}}]${newline}日誌來源:${logger}${newline}日誌資訊:${message}${newline}錯誤資訊:${exception:format=tostring}${newline}==============================================================${newline}" /> <target xsi:type="File" name="info" fileName="${basedir}\log\nlog-info-${shortdate}.log" layout="${longdate} >> [日誌級別:${uppercase:${level}}]${newline}日誌資訊:${message}${newline}" /> </targets> <!-- rules to map from logger name to target --> <rules> <!--<logger name="*" minlevel="Trace" writeTo="allfile" />--> <logger name="*" level="Error" writeTo="error" /> <logger name="*" level="Info" writeTo="info" /> <!--Skip non-critical Microsoft logs and so log only own logs--> <logger name="Microsoft.*" maxLevel="Info" final="true" /> <!-- BlackHole without writeTo --> <!-- <logger name="*" minlevel="Trace" writeTo="ownFile-web" /> --> </rules> </nlog>