zoukankan      html  css  js  c++  java
  • AspNetCore使用NLog做日志记录

    安装包

    安装nuget包NLog.Web.AspNetCoreMicrosoft.Extensions.Logging

    配置文件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"
          xsi:schemaLocation="http://www.nlog-project.org/schemas/NLog.xsd NLog.xsd"
          autoReload="true"
          throwExceptions="false"
          internalLogLevel="Off" internalLogFile="c:	emp
    log-internal.log">
    	<variable name="myvar" value="myvalue" />
    	<extensions>
    		<add assembly="NLog.Web.AspNetCore" />
    	</extensions>
    	<targets>
    		<!--Error保存至文件-->
    		<target name="error_file" xsi:type="File" maxArchiveFiles="30"  encoding="utf-8"
                fileName="${basedir}/Logs/${date:yyyyMMdd}_Error.log"
                archiveFileName="${basedir}/Logs/${date:yyyyMMdd}_Error.{#}.log"
                archiveDateFormat="yyyyMMdd"
                archiveAboveSize="104857600"
                archiveNumbering="Sequence"
                layout="${date:yyyy-MM-dd HH:mm:ss} ${message} ${onexception:${exception:format=tostring} ${newline} ${stacktrace}" />
    		<!--Trace保存至文件-->
    		<target name="trace_file" xsi:type="File" maxArchiveFiles="30" encoding="utf-8"
                fileName="${basedir}/Logs/${date:yyyyMMdd}_Trace.log"
                archiveFileName="${basedir}/Logs/${date:yyyyMMdd}_Trace.{#}.log"
                archiveDateFormat="yyyyMMdd"
                archiveAboveSize="104857600"
                archiveNumbering="Sequence"
                layout="${date:yyyy-MM-dd HH:mm:ss} ${uppercase:${level}}: ${message}" />
    	</targets>
    
    	<rules>
    		<logger name="*" minlevel="Info" maxlevel="Warn" writeTo="trace_file" />
    		<logger name="*" minlevel="Error" writeTo="error_file" />
    	</rules>
    </nlog>
    

    Program修改

    正常的依赖注入使用日志

  • 相关阅读:
    Jmeter学习笔记-初级
    python selenium利用 126.com id自动生成
    Python代码无法保存
    卸载python2.7出错
    python pip用法
    pycharm 连接数据库
    Python打包PyPI上传实践
    Python实现Api的Mock测试
    抓包工具使用
    Jenkins+Ant可持续集成Jmeter脚本
  • 原文地址:https://www.cnblogs.com/az4215/p/15217182.html
Copyright © 2011-2022 走看看