NLog配置
![](https://img2023.cnblogs.com/blog/1535942/202303/1535942-20230314101106833-663334998.png)
<?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" throwExceptions="false" internalLogLevel="Off" internalLogFile="NlogRecords.log"> <!--Nlog内部日志记录为Off关闭。除非纠错,不可以设为Trace否则速度很慢,起码Debug以上--> <!--<extensions> <add assembly="NLog.Web.AspNetCore" /> </extensions>--> <rules> <!--跳过所有级别的Microsoft组件的日志记录--> <logger name="Microsoft.*" final="true" /> <!-- BlackHole without writeTo --> <!--只通过数据库记录日志,如果给了name名字,cs里用日志记录的时候,取logger需要把name当做参数--> <logger name="logdb" writeTo="log_database" /> <logger name="logfile" writeTo="log_file" /> </rules> <targets> <!--通过数据库记录日志 配置--> <!--<target name="log_database" xsi:type="Database" dbProvider="Microsoft.Data.SqlClient.SqlConnection, Microsoft.Data.SqlClient" connectionString="Server=.;uid=sa;pwd=y123123!@;Database=SunnyAR;MultipleActiveResultSets=true;pooling=true;min pool size=5;max pool size=32767;connect timeout=20;"> <commandText> INSERT INTO SysNLogRecords (LogDate,LogLevel,LogType,LogTitle,Logger,Message,MachineName,MachineIp,NetRequestMethod ,NetRequestUrl,NetUserIsauthenticated,NetUserAuthtype,NetUserIdentity,Exception) VALUES (@LogDate,@LogLevel,@LogType,@LogTitle,@Logger,@Message,@MachineName,@MachineIp,@NetRequestMethod ,@NetRequestUrl,@NetUserIsauthenticated,@NetUserAuthtype,@NetUserIdentity,@Exception); </commandText> <parameter name="@LogDate" layout="${date}" /> <parameter name="@LogLevel" layout="${level}" /> <parameter name="@LogType" layout="${event-properties:item=LogType}" /> <parameter name="@LogTitle" layout="${event-properties:item=LogTitle}" /> <parameter name="@Logger" layout="${logger}" /> <parameter name="@Message" layout="${message}" /> <parameter name="@MachineName" layout="${machinename}" /> <parameter name="@MachineIp" layout="${aspnet-request-ip}" /> <parameter name="@NetRequestMethod" layout="${aspnet-request-method}" /> <parameter name="@NetRequestUrl" layout="${aspnet-request-url}" /> <parameter name="@NetUserIsauthenticated" layout="${aspnet-user-isauthenticated}" /> <parameter name="@NetUserAuthtype" layout="${aspnet-user-authtype}" /> <parameter name="@NetUserIdentity" layout="${aspnet-user-identity}" /> <parameter name="@Exception" layout="${exception:tostring}" /> </target>--> <target xsi:type="File" name="log_file" fileName="${gdc:item=logDirectory}/Log/${shortdate}.log" encoding="utf-8" layout="${date}|${event-properties:item=EventId.Id}|${logger}|${uppercase:${level}}| ${message} ${exception:format=tostring}" /> <!--<target xsi:type="File" name="log_file" fileName="D:/AR_Test/Log/${shortdate}.log" encoding="utf-8" layout="${date}|${event-properties:item=EventId.Id}|${logger}|${uppercase:${level}}| ${message} ${exception:format=tostring}" />--> <!--<target xsi:type="File" name="log_file" fileName="${basedir}/App_Data/nlog/${date:format=yyyy-MM}/${level}-${shortdate}.csv" encoding="utf-8"> <layout xsi:type="CSVLayout"> <column name="date" layout="${date}" /> <column name="LogTitle" layout="${event-properties:item=LogTitle}" /> <column name="message" layout="${message}" /> <column name="exception" layout="${exception:format=tostring}" /> <column name="machinename" layout="${machinename}" /> <column name="appdomain" layout="${appdomain}" /> <column name="assembly-version" layout=" ${assembly-version}" /> <column name="basedir" layout="${basedir}" /> <column name="callsite" layout="${callsite}" /> <column name="counter" layout="${counter}" /> <column name="nlogdir" layout="${nlogdir}" /> <column name="processid" layout="${processid}" /> <column name="processname" layout="${processname}" /> <column name="specialfolder" layout="${specialfolder}" /> <column name="stacktrace" layout="${stacktrace}" /> <column name="longdate" layout="${longdate}" /> <column name="event-properties" layout="${event-properties:item=EventId_Id}" /> <column name="uppercase" layout="${uppercase:${level}}" /> <column name="logger" layout="${logger}" /> <column name="url" layout="${aspnet-request-url}" /> <column name="action" layout="${aspnet-mvc-action}" /> </layout> </target>--> </targets> </nlog>
private void button4_Click(object sender, EventArgs e) { //SunnyBaseEntity sunnyBaseEntity = new SunnyBaseEntity(); //SpecMeasure(ref sunnyBaseEntity);//计算色坐标 string path = ConfigurationManager.AppSettings["GlobalSavePath"]; NLog.GlobalDiagnosticsContext.Set("logDirectory", path); LogManager.Configuration = LogManager.Configuration; LogHelper.Log(NLog.LogLevel.Info, "", "", "B-10%APL烧录信息:"); NLogUtil.WriteFileLog(NLog.LogLevel.Info, LogType.Form, "", "ceshi"); }