.Net Core NLog 配置

简介: .Net Core NLog 配置
using NLog;
private static Logger logger = LogManager.GetCurrentClassLogger(); //初始化日志类

 

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:\temp\nlog-internal.log">
  <variable name="logDirectory" value="${basedir}/logs"/>
  <variable name="traceVal" value="${date:format=HH\:mm\:ss} [Thread-${threadname:whenEmpty=${threadid}}]|${message}"/>
  <variable name="layoutVal" value="${date:format=HH\:mm\:ss} [Thread-${threadname:whenEmpty=${threadid}}]|${uppercase:${level}}|${callsite:fileName=True}${newline}${message}${newline}${exception}"/>
  <variable name="consoleVal" value="${date:format=HH\:mm\:ss}|${pad:padding=5:inner=${level:uppercase=true}}|${message}"/>
  <targets>
    <target name="console" xsi:type="ColoredConsole" useDefaultRowHighlightingRules="false"
            layout="${consoleVal}" >
      <highlight-row condition="level == LogLevel.Debug" foregroundColor="DarkGray" />
      <highlight-row condition="level == LogLevel.Info" foregroundColor="Gray" />
      <highlight-row condition="level == LogLevel.Warn" foregroundColor="Yellow" />
      <highlight-row condition="level == LogLevel.Error" foregroundColor="Red" />
      <highlight-row condition="level == LogLevel.Fatal" foregroundColor="Red" backgroundColor="White" />
    </target>
    <target xsi:type="File"
      name="logfile"
      fileName="${logDirectory}/info_${shortdate}.log"
      keepFileOpen="false"
      layout="${layoutVal}" />
    <target xsi:type="File"
      name="warnfile"
      fileName="${logDirectory}/warn_${shortdate}.log"
      keepFileOpen="false"
      layout="${layoutVal}" />
    <target xsi:type="File"
      name="errfile"
      fileName="${logDirectory}/error_${shortdate}.log"
      keepFileOpen="false"
      layout="${layoutVal}" />
    <target xsi:type="File"
      name="debugfile"
      fileName="${logDirectory}/debug_${shortdate}.log"
      keepFileOpen="false"
      layout="${layoutVal}" />
    <target xsi:type="File"
            name="tracefile"
            fileName="${logDirectory}/trace_${shortdate}.log"
            keepFileOpen="false"
            layout="${traceVal}" />
  </targets>
  <rules>
    <logger name="*" writeTo="console" />
    <logger name="*" level="Trace" writeTo="tracefile"/>
    <logger name="*" level="Error" writeTo="errfile" />
    <logger name="*" level="Warn" writeTo="warnfile"/>
    <logger name="*" minlevel="Info" writeTo="logfile"/>
    <logger name="*" minlevel="Debug" writeTo="debugfile" />
  </rules>
</nlog>
目录
相关文章
|
5月前
|
XML 开发框架 .NET
ASP.NET COR3.1 集成日志插件NLog
ASP.NET COR3.1 集成日志插件NLog
72 0
|
2月前
|
开发框架 JSON 安全
分享一个 .NET Core 使用选项方式读取配置内容的详细例子
分享一个 .NET Core 使用选项方式读取配置内容的详细例子
|
2月前
【Azure 应用服务】App Service 配置 Application Settings 访问Storage Account得到 could not be resolved: '*.file.core.windows.net'的报错。没有解析成对应中国区 Storage Account地址 *.file.core.chinacloudapi.cn
【Azure 应用服务】App Service 配置 Application Settings 访问Storage Account得到 could not be resolved: '*.file.core.windows.net'的报错。没有解析成对应中国区 Storage Account地址 *.file.core.chinacloudapi.cn
|
2月前
|
开发框架 .NET API
如何在 ASP.NET Core Web Api 项目中应用 NLog 写日志?
如何在 ASP.NET Core Web Api 项目中应用 NLog 写日志?
119 0
|
2月前
|
监控 程序员 数据库
分享一个 .NET Core Console 项目中应用 NLog 写日志的详细例子
分享一个 .NET Core Console 项目中应用 NLog 写日志的详细例子
|
2月前
|
开发框架 NoSQL .NET
使用 Asp.net core webapi 集成配置系统,提高程序的灵活和可维护性
使用 Asp.net core webapi 集成配置系统,提高程序的灵活和可维护性
|
2月前
|
开发框架 前端开发 JavaScript
前后端分离,Asp.net core webapi 如何配置跨域
前后端分离,Asp.net core webapi 如何配置跨域
|
5月前
|
开发框架 JSON .NET
.Net4.0 Web.config 配置实践
.Net4.0 Web.config 配置实践
|
5月前
|
IDE 前端开发 JavaScript
【C#】C# 开发环境配置(Rider 一个.NET 跨平台集成开发环境)
【1月更文挑战第26天】【C#】C# 开发环境配置(Rider 一个.NET 跨平台集成开发环境)
|
5月前
|
开发框架 .NET PHP
Web Deploy配置并使用Visual Studio进行.NET Web项目发布部署
Web Deploy配置并使用Visual Studio进行.NET Web项目发布部署
107 1