码迷,mamicode.com
首页 > 其他好文 > 详细

nlog 配置

时间:2019-05-09 10:39:16      阅读:114      评论:0      收藏:0      [点我收藏+]

标签:oid   pre   contents   text   case   space   threading   hit   logger   

using NLog;
using NLog.Config;
using NLog.Targets;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Dben.CommonLib
{
    public static class NLogDefaultConfig
    {
        public static void InitNlogInfo()
        {
            const string layout = @"${date:format=HH\:mm\:ss}[${logger}]${message}";

            var consoleTarget = new ColoredConsoleTarget()
            {
                Layout = layout
            };

            var fileTarget = new FileTarget()
            {
                FileName = "${basedir}/logs/${logger}/${shortdate}.txt",
                Layout = layout
            };

            var config = new LoggingConfiguration();
            config.AddTarget("console", consoleTarget);
            config.AddTarget("file", fileTarget);

            config.LoggingRules.Add(new LoggingRule("*", LogLevel.Info, consoleTarget));
            config.LoggingRules.Add(new LoggingRule("*", LogLevel.Info, fileTarget));

            LogManager.Configuration = 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"
      internalLogLevel="Trace"
      internalLogFile="D:\LogFiles\nlog.internal.log.txt">
  
  <!--<variable name="gmailUsername" value="${trim-whitespace:${file-contents:${basedir}/gmailusername.txt}}" />
  <variable name="gmailPassword" value="${trim-whitespace:${file-contents:${basedir}/gmailpassword.txt}}" />-->

  <!-- define various log targets -->
  <targets>
    <!-- write logs to file -->
    <target xsi:type="File" name="file" fileName="D:\LogFiles\${logger}\${shortdate}.log"
            layout="${longdate} ${aspnet-request:servervariable=URL} ${uppercase:${level}} ${message}" />
   </targets>

  <rules>
    <logger name="*" minlevel="Trace" writeTo="file" />
  </rules>
</nlog>

二选一

nlog 配置

标签:oid   pre   contents   text   case   space   threading   hit   logger   

原文地址:https://www.cnblogs.com/zhuwansu/p/10836984.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!