环境:win7 64位, VS2010

1、首先用VS2010创建命令行工程NLogDemo

2、在程序包管理器控制台中输入:Install-Package NLog -Version 4.4.12

  这句是怎么来的,要是你用过nuget包管理工具,那就可以跳过这里的说明了。

  要使用nuget添加NLog包到项目中。请看下图。

然后在程序包管理工具控制台下输入:Install-Package NLog -Version 4.4.12

再看看Install-Package NLog -Version 4.4.12这个怎么找。

打开百度搜索:Nuget

然后在Nuget官网上搜索栏输入:Nlog 回车

选择第一项Nlog

然后在 Version History 下选择 4.4.12 这个版本

至于为什么选择这个版本,因为这个版本下载的次数多。嘿嘿,没办法,随大流。当然还要看这个版本包的依赖项

这个包的没有什么特殊依赖项,所以可以使用。然后拷贝

这里这句话就是要我们要找的。是不是挺简单的。

当我们在包程序包管理器控制台中输入:Install-Package NLog -Version 4.4.12 然后按下回车键,VS IDE 会自动到 nuget.org 这里下载依赖包。

NLog 包添加完之后,还要添加 NLog.config(4.4.12)(Install-Package NLog.Config -Version 4.4.12) 这个包,按道理 NLog 和 NLog.config 应该一起的,

突然从某个版本开始分开了,要单独添加。具体情况可以去官网看介绍:https://nlog-project.org/

添加 NLog.config 的方法跟上面添加 NLog 的方法一样。

3、简单封装 Log

  添加类Log.cs到工程中,修改代码如下:

public sealed class Log
{
private static NLog.Logger _logger = NLog.LogManager.GetCurrentClassLogger(); private Log() { } public static void Trace(string strMsg)
{
_logger.Trace(strMsg);
} public static void Debug(string strMsg)
{
_logger.Debug(strMsg);
} public static void Info(string strMsg)
{
_logger.Info(strMsg);
} public static void Warn(string strMsg)
{
_logger.Warn(strMsg);
} public static void Error(string strMsg)
{
_logger.Error(strMsg);
} public static void Fatal(string strMsg)
{
_logger.Fatal(strMsg);
} }

4、修改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"/>
-->
<variable name="fileFormat"
value="
${newline}date: ${date}
${newline}level: ${level}
${newline}logger: ${logger}
${newline}machinename: ${machinename}
${newline}message: ${message}
${newline}appdomain: ${appdomain}
${newline}assembly-version: ${assembly-version}
${newline}basedir: ${basedir}
${newline}callsite: ${callsite}
${newline}counter: ${counter}
${newline}nlogdir: ${nlogdir}
${newline}processid: ${processid}
${newline}processname: ${processname}
${newline}specialfolder: ${specialfolder}
${newline}stacktrace: ${stacktrace}
${newline}------------------------------------------------------------" /> <!--
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}" />
--> <target name="file" xsi:type="File"
fileName="${basedir}/Logs/${date:format=yyyy-MM}/${shortdate}.log"
layout="${fileFormat}"
maxArchiveFiles=""
archiveAboveSize="" /> </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" />
--> <!--
Level Example
Fatal Highest level: important stuff down
Error For example application crashes / exceptions.
Warn Incorrect behavior but the application can continue
Info Normal behavior like mail sent, user updated profile etc.
Debug Executed queries, user authenticated, session expired
Trace Begin method X, end method X etc
-->
<!--
Logging 水平分为以下等级“Trace<<Debug<<Info<<Warn<<Error<<Fatal ”,如果我们选择Info值,则Trace和Debug等级的信息不会被输出。
-->
<logger name="*" minlevel="Trace" writeTo="file"/>
</rules>
</nlog>

简单解释:

variable  log文件的内容输出格式
targets 目标文件(要生成的Log文件)的配置(文件名、格式变量、文件个数、文件大小等等)
rules 规则,也就是俗话说的Log输出级别

以上内容不进行过多解释了,再多解释也不如官网的说明。详细介绍请看官网:https://github.com/nlog/NLog/wiki/Configuration-file#configuration-file-format更多配置文件可以参考: https://github.com/jkowalski/NLog/tree/master/examples/targets/Configuration%20File

5、使用Log输出日志到文件,简单示例代码如下

class Program
{
static void Main(string[] args)
{
RunTest(); Console.WriteLine("Press a key end ...");
Console.ReadKey(true);
} static void RunTest()
{
for (int i = ; i < ; i++)
{
Log.Info(string.Format("{0}", i + )); System.Threading.Thread.Sleep();
}
} }

输出路径结构

输出文件内容:

以上内容输出格式可以在NLog.config中根据需求进行裁剪。

demo下载

C# 使用NLog记录日志入门操作的更多相关文章

  1. EF+LINQ事物处理 C# 使用NLog记录日志入门操作 ASP.NET MVC多语言 仿微软网站效果(转) 详解C#特性和反射(一) c# API接受图片文件以Base64格式上传图片 .NET读取json数据并绑定到对象

    EF+LINQ事物处理   在使用EF的情况下,怎么进行事务的处理,来减少数据操作时的失误,比如重复插入数据等等这些问题,这都是经常会遇到的一些问题 但是如果是我有多个站点,然后存在同类型的角色去操作 ...

  2. [转]C# 使用Nlog记录日志到数据库

    本文转自:http://www.cnblogs.com/weixing/archive/2013/04/26/3044422.html 摘要]Nlog是一个很不错的.NET日志记录组件,它可以将日志输 ...

  3. .NET中使用NLog记录日志

    以前小编记录日志使用的是Log4Net,虽然好用但和NLog比起来稍显复杂.下面小编就和大伙分享一下NLog的使用方式. 引用NLog.Config 在使用NLog之前,我们要首先添加对NLog.Co ...

  4. Nlog 记录日志到 sqlite

    最近研究了一下Nlog这个日志框架,这里记录一下如何将日志写到sqlite中. 第一步:使用NuGet获取Nlog和Sqlite 第二步:在sqlite中创建一个database,这里我用了SQLit ...

  5. C# 使用Nlog记录日志到数据库 使用LogEventInfo类获取,命名空间名称、类名、方法名

    原文地址:http://dotnet.9sssd.com/csbase/art/793 [摘要]Nlog是一个很不错的.NET日志记录组件,它可以将日志输出到控件台,保存到文本,也可以很方便的记录到数 ...

  6. spring boot 入门操作(二)

    spring boot入门操作 使用FastJson解析json数据 pom dependencies里添加fastjson依赖 <dependency> <groupId>c ...

  7. spring boot 入门操作(三)

    spring boot入门操作 devtools热部署 pom dependencies里添加依赖 <dependency> <groupId>org.springframew ...

  8. Mysql的二进制安装和基础入门操作

    前言:Mysql数据库,知识非常的多,要想学精学通这块知识,估计也要花费和学linux一样的精力和时间.小编也是只会些毛皮,给大家分享一下~ 一.MySQL安装 (1)安装方式: 1 .程序包yum安 ...

  9. java之servlet入门操作教程一续

    本节主要是在java之servlet入门操作教程一  的基础上使用myeclipse实现自动部署的功能 准备: java之servlet入门操作教程一 中完成myFirstServlet项目的创建: ...

随机推荐

  1. 交叉编译和安装ARM板(RK3288)和Linux 3.10上的RTL8188无线网卡驱动

    插入无线网卡,输入ifconfig,发现没有检测到网卡. 输入lsusb,查看无线网卡型号. 我用的无线网卡是EDUP的网卡,包装盒里有一张驱动光盘,把光盘里linux下的驱动目录复制下来.如果没有驱 ...

  2. 【开源小软件 】Bing每日壁纸 V1.2.1

    Bing每日壁纸发布V1.2版本,下载地址Release V1.2.1 该小软件可以自动获取Bing的精美图片设置为壁纸,并且支持随机切换历史壁纸,查看壁纸故事. 本次新增国际化支持,以及桌面widg ...

  3. linux安装目录

    Linux 的软件安装目录是也是有讲究的,理解这一点,在对系统管理是有益的 /usr:系统级的目录,可以理解为C:/Windows/,/usr/lib理解为C:/Windows/System32./u ...

  4. Codeforces 739C Alyona and towers 线段树

    Alyona and towers 这个题写起来真的要人命... 我们发现一个区间被加上一个d的时候, 内部的结构是不变的, 改变的只是左端点右端点的值, 这样就能区间合并了. 如果用差分的话会简单一 ...

  5. BZOJ1853 [Scoi2010]幸运数字 容斥原理

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ1853 题意概括 求一个区间范围内,近似幸运数字的个数. 定义: 幸运数字:仅由6或者8组成的数字. ...

  6. Python3中urllib使用介绍

    Py2.x: Urllib库 Urllin2库 Py3.x: Urllib库 变化: 在Pytho2.x中使用import urllib2——-对应的,在Python3.x中会使用import url ...

  7. 附004.Docker Compose环境变量说明

    一 环境变量概述 Compose支持在docker-compose执行命令的文件夹中,名为.env的环境变量配置文件声明默认环境变量. 二 定义变量 2.1 语法规则 Compose建议env文件中的 ...

  8. 潭州课堂25班:Ph201805201 爬虫基础 第十四课 js破解 (课堂笔记)

    打断点 找要的数据 鼠标的点击事件 新浪微博登录 表单提交分析 : 先佃输入错误密码开始调式 f10 往下走, f11 进入函数 sh + f11 跳出函数 # -*- coding: utf-8 - ...

  9. stm32与HC-SR04超声波传感器测距

    首先,先来看一下这个模块的基本功能和原理. HC-SR04超声波测距模块可提供2cm-400cm的非接触式距离感测功能,测距精度可达高到3mm:模块包括超声波发射器.接收器与控制电路.像智能小车的测距 ...

  10. Docker machine(Docker 虚拟机)

    安装docker [root@lianxi ~]# yum -y install docker 启动docker [root@lianxi ~]# systemctl start docker 下载D ...