• NLog使用


    NLog.Config

    1. <?xml version="1.0" encoding="utf-8" ?>
    2. <nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
    3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4. xsi:schemaLocation="http://www.nlog-project.org/schemas/NLog.xsd NLog.xsd"
    5. autoReload="true"
    6. throwExceptions="false"
    7. internalLogLevel="Off"
    8. internalLogFile="d:\nlog\nlog-internal.log">
    9. <!-- optional, add some variables
    10. https://github.com/nlog/NLog/wiki/Configuration-file#variables
    11. -->
    12. <!--<variable name="myvar" value="myvalue"/>-->
    13. <variable name="logDir" value="${basedir}/nlog"/>
    14. <variable name="logFileName" value="${date:format=yyyyMMdd}.txt"/>
    15. <variable name="logArchiveFileName" value="${date:format=yyyyMMdd}_{#}.txt"/>
    16. <variable name="logLayout" value="Logger:${logger}${newline}Date:${longdate}${newline}Level:${uppercase:${level}}${newline}Message:${message} ${newline}${onexception:Exception:${exception:format=toString}${newline}}" />
    17. <!--
    18. See https://github.com/nlog/nlog/wiki/Configuration-file
    19. for information on customizing logging rules and outputs.
    20. -->
    21. <targets>
    22. <!--
    23. add your targets here
    24. See https://github.com/nlog/NLog/wiki/Targets for possible targets.
    25. See https://github.com/nlog/NLog/wiki/Layout-Renderers for the possible layout renderers.
    26. -->
    27. <!--
    28. Write events to a file with the date in the filename.
    29. <target xsi:type="File" name="f" fileName="${basedir}/logs/${shortdate}.log"
    30. layout="${longdate} ${uppercase:${level}} ${message}" />
    31. -->
    32. <target xsi:type="File" name="info"
    33. layout="${logLayout}"
    34. fileName="${logDir}/info/${logFileName}"
    35. archiveFileName="${logDir}/info/${logArchiveFileName}"
    36. archiveAboveSize="10485760"
    37. archiveNumbering="Sequence"
    38. maxArchiveFiles="100"
    39. concurrentWrites="true"
    40. keepFileOpen="true"
    41. openFileCacheTimeout="30"
    42. encoding="UTF-8" />
    43. <target xsi:type="File" name="debug"
    44. layout="${logLayout}"
    45. fileName="${logDir}/debug/${logFileName}"
    46. archiveFileName="${logDir}/debug/${logArchiveFileName}"
    47. archiveAboveSize="10485760"
    48. archiveNumbering="Sequence"
    49. maxArchiveFiles="100"
    50. concurrentWrites="true"
    51. keepFileOpen="true"
    52. openFileCacheTimeout="30"
    53. encoding="UTF-8" />
    54. <target xsi:type="File" name="error"
    55. layout="${logLayout}"
    56. fileName="${logDir}/error/${logFileName}"
    57. archiveFileName="${logDir}/error/${logArchiveFileName}"
    58. archiveAboveSize="10485760"
    59. archiveNumbering="Sequence"
    60. maxArchiveFiles="100"
    61. concurrentWrites="true"
    62. keepFileOpen="true"
    63. openFileCacheTimeout="30"
    64. encoding="UTF-8" />
    65. </targets>
    66. <rules>
    67. <!-- add your logging rules here -->
    68. <!--
    69. Write all events with minimal level of Debug (So Debug, Info, Warn, Error and Fatal, but not Trace) to "f"
    70. <logger name="*" minlevel="Debug" writeTo="f" />
    71. -->
    72. <logger name="*" minlevel="Info" maxlevel="Info" writeTo="info" />
    73. <logger name="*" minlevel="Debug" maxlevel="Debug" writeTo="debug" />
    74. <logger name="*" minlevel="Error" maxlevel="Error" writeTo="error" />
    75. </rules>
    76. </nlog>

    不要忘记

     测试代码

    1. using System;
    2. using System.Collections.Generic;
    3. using System.ComponentModel;
    4. using System.Data;
    5. using System.Drawing;
    6. using System.Linq;
    7. using System.Text;
    8. using System.Windows.Forms;
    9. using NLog;
    10. namespace NlogTest
    11. {
    12. public partial class Form1 : Form
    13. {
    14. public Form1()
    15. {
    16. InitializeComponent();
    17. }
    18. private Logger _log = NLog.LogManager.GetCurrentClassLogger();
    19. private void button1_Click(object sender, EventArgs e)
    20. {
    21. _log.Info("信息");
    22. _log.Error("异常");
    23. int a = 0;
    24. int b = 0;
    25. try
    26. {
    27. int c = a / b;
    28. }
    29. catch (Exception ex)
    30. {
    31. _log.Error(ex,"操作异常");
    32. }
    33. }
    34. }
    35. }

    效果

    Demo下载icon-default.png?t=M5H6https://download.csdn.net/download/lw112190/85812952

  • 相关阅读:
    3038. 相同分数的最大操作数目 I(Rust模拟击败100%Rust用户)
    MySQL笔记之一致性视图与MVCC实现
    35岁的测试工程师被炒,中年危机真的有这么可怕吗?
    Three.js入门详解
    软件工程与计算总结(九)软件体系结构基础
    Revit中为房间添加填充图例和“构件快速上色”
    【大数据面试题】014 Flink CDC 用过吗,请简要描述
    java计算机毕业设计基于springboot电商项目(附源码讲解)
    Qt-OpenCV学习笔记--边缘检测--Canny()
    物联网对接协议
  • 原文地址:https://blog.csdn.net/lw112190/article/details/125505971