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修改

    正常的依赖注入使用日志

  • 相关阅读:
    发现了一个开源的sip软电话项目(C#)
    有没有.Net下的开源工作流框架推荐
    C#生成32位MD5加密
    web安全问题与Safe3 Web应用防火墙
    Safe3网页防篡改系统 v4.0
    C和C++混合编程问题
    Safe3 WEB安全网关linux 3.1版
    Dns信息收集工具集合
    lizamoon.com挂马解决办法
    Safe3网站安全网关 3.1发布
  • 原文地址:https://www.cnblogs.com/az4215/p/15217182.html
Copyright © 2011-2022 走看看