• [.Net Core]


    1. 使用 Nuget 安装 NLog。

    2. 在 Sql Server 中创建 NLog 数据表。

    CREATE TABLE [dbo].[NLogInfo](  
        [LogId] [int] IDENTITY(1,1) NOT NULL,  
        [Date] [datetime] NOT NULL,  
        [Origin] [nvarchar](100) NULL,  
        [Level] [nvarchar](50) NULL,  
        [Message] [nvarchar](max) NULL,  
        [Detail] [nvarchar](max) NULL,  
    ) ON [PRIMARY]

    3. 创建并配置 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"
          throwExceptions="false"
          internalLogLevel="Error"
          internalLogFile="Nlog.log">
      <variable name="detailTemplate" value="
    ${newline}date: ${date}
    ${newline}level: ${level}
    ${newline}logger: ${logger}
    ${newline}machinename: ${machinename}
    ${newline}message: ${message}
    ${newline}appdomain: ${appdomain}
    ${newline}assembly-version: ${assembly-version}
    ${newline}basedir: ${basedir}
    ${newline}callsite: ${callsite}
    ${newline}callsite-linenumber: ${callsite-linenumber}
    ${newline}counter: ${counter}
    ${newline}nlogdir: ${nlogdir}
    ${newline}processid: ${processid}
    ${newline}processname: ${processname}
    ${newline}specialfolder: ${specialfolder}
    ${newline}stacktrace: ${stacktrace}
    ${newline}exception: ${exception:format=tostring}" />
      <targets>
        <target name="blackhole" xsi:type="Null" />
        <target name="database" xsi:type="Database">
          <connectionString>${var:connectionString}</connectionString>
          <commandText>
            insert into NLogInfo([Date],[origin],[Level],[Message],[Detail]) values (getdate(), @origin, @logLevel, @message,@detail);
          </commandText>
          <parameter name="@origin" layout="${callsite}" />
          <parameter name="@logLevel" layout="${level}" />
          <parameter name="@message" layout="${message}" />
          <parameter name="@detail" layout="${detailTemplate}" />
        </target>
      </targets>
      <rules>
        <logger name="*" minlevel="Warn" writeTo="database" />
      </rules>
    </nlog>

    4. 在 Startup 中配置 NLog

    env.ConfigureNLog("nlog.config");
    LogManager.Configuration.Variables["connectionString"] = Configuration.GetConnectionString("DefaultConnection");
    loggerFactory.AddNLog();

    5. 测试:手工记 Log + 全局异常捕获。

    参考资料

    https://blog.csdn.net/u013667895/article/details/79067828
    https://www.cnblogs.com/chen8854/p/6800158.html
    https://stackoverflow.com/questions/5346336/nlog-configuration-across-appdomains

  • 相关阅读:
    Android 开发学习进程0.19 webview 的使用
    2020年4到6月—7家公司面试总结(3家已拿offer)
    iOS今日头条第3轮面试回忆
    [搬运]Dart之枚举中使用扩展
    Proguard 常用规则
    shiro安全框架
    Android服务的AIDL跨进程(程序)操作
    Android——服务的实例,银行服务
    Android四大组件之服务————服务的生命周期和启动方式
    Android 程序间的广播和Manifest找不到(解决方法)
  • 原文地址:https://www.cnblogs.com/jinzesudawei/p/9219224.html
Copyright © 2020-2023  润新知