C# NLog日志框架


1、首先下載NLog框架,在vs NuGet中搜索NLog,下載安裝NLog.Config

2、配置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">

  <!-- optional, add some variables
  https://github.com/nlog/NLog/wiki/Configuration-file#variables
  -->
  <variable name="myvar" value="myvalue"/>

  <!--
  See https://github.com/nlog/nlog/wiki/Configuration-file
  for information on customizing logging rules and outputs.
   -->
  <targets>

    <!--
    add your targets here
    See https://github.com/nlog/NLog/wiki/Targets for possible targets.
    See https://github.com/nlog/NLog/wiki/Layout-Renderers for the possible layout renderers.
    -->

    <!--
    Write events to a file with the date in the filename.  
    -->
    <target xsi:type="File" name="f" fileName="${basedir}/logs/${shortdate}.log"
            layout="${longdate} ${uppercase:${level}} ${message}" encoding="UTF-8"/>

    <target xsi:type="Console" name="c"
            layout="${longdate} ${uppercase:${level}} ${message}" encoding="UTF-8"/>
    
  </targets>

  <rules>
    <!-- add your logging rules here -->

    <!--
    Write all events with minimal level of Debug (So Debug, Info, Warn, Error and Fatal, but not Trace)  to "f"  
    -->

    <logger name="*" minlevel="Debug" writeTo="f"/>
    <logger name="*" minlevel="Debug" writeTo="c"/>
  </rules>
</nlog>

3、寫靜態日志類LogUtil

  1   public class LogUtils
  2     {
  3         private static readonly Logger log = LogManager.GetLogger("*");
  4         /// <summary>
  5         /// 保存info級別的信息
  6         /// </summary>
  7         /// <param name="message"></param>
  8         public static void infoWrite(string message)
  9         {
 10             log.Info(message);
 11         }
 12         /// <summary>
 13         /// 保存info級別的信息
 14         /// </summary>
 15         /// <param name="className">類名</param>
 16         /// <param name="methodName">方法名</param>
 17         /// <param name="content">日志內容</param>
 18         public static void infoWrite(string className, string methodName, string content)
 19         {
 20             string message = "className:{" + className + "}, methodName:{" + methodName + "}, content:{" + content + "}";
 21             log.Info(message);
 22         }
 23 
 24 
 25         /// <summary>
 26         /// 保存error級別信息
 27         /// </summary>
 28         /// <param name="error"></param>
 29         public static void errorWrite(string error)
 30         {
 31             log.Error(error);
 32         }
 33 
 34         /// <summary>
 35         /// 保存error級別信息
 36         /// </summary>
 37         /// <param name="className">類名</param>
 38         /// <param name="methodName">方法名</param>
 39         /// <param name="content">日志內容</param>
 40         public static void errorWrite(string className, string methodName, string content)
 41         {
 42             string message = "className:{" + className + "}, methodName:{" + methodName + "}, content:{" + content + "}";
 43             log.Error(message);
 44         }
 45 
 46         /// <summary>
 47         /// 保存debug級別信息
 48         /// </summary>
 49         /// <param name="message"></param>
 50         public static void debugWrite(string message)
 51         {
 52             log.Debug(message);
 53         }
 54 
 55         /// <summary>
 56         /// 保存debug級別信息
 57         /// </summary>
 58         /// <param name="className">類名</param>
 59         /// <param name="methodName">方法名</param>
 60         /// <param name="content">日志內容</param>
 61         public static void debugWrite(string className, string methodName, string content)
 62         {
 63             string message = "className:{" + className + "}, methodName:{" + methodName + "}, content:{" + content + "}";
 64             log.Debug(message);
 65         }
 66 
 67         /// <summary>
 68         /// 刪除2個月前的日志文件
 69         /// </summary>
 70         public static void deleteLogFile(string logPath)
 71         {          
 72             if (!Directory.Exists(logPath))
 73             {
 74                 return;
 75             }
 76             DirectoryInfo folder = new DirectoryInfo(logPath);
 77             FileInfo[] files = folder.GetFiles("*.log");
 78             if (files == null)
 79             {
 80                 return;
 81             }
 82 
 83             foreach (FileInfo file in files)
 84             {
 85                 //文件創建時間
 86                 DateTime fileCreateTime = file.LastWriteTime;
 87                 //當前時間
 88                 DateTime now = DateTime.Now;
 89                 int createMonth = fileCreateTime.Month;
 90                 int nowMonth = now.Month;
 91 
 92                 int distance = nowMonth - createMonth;
 93                 distance = distance >= 0 ? distance : (distance + 12);
 94 
 95                 if (distance < 3)
 96                 {
 97                     //小於三個月不刪除
 98                     continue;
 99                 }
100 
101                 try
102                 {
103                     File.Delete(file.FullName);
104                 }
105                 catch
106                 {
107                     throw new Exception("刪除日志文件出現異常");
108                 }
109 
110             }
111         }
112 
113     }

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM