C# 使用NLog记录日志入门操作
环境: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中根据需求进行裁剪。
C# 使用NLog记录日志入门操作的更多相关文章
- EF+LINQ事物处理 C# 使用NLog记录日志入门操作 ASP.NET MVC多语言 仿微软网站效果(转) 详解C#特性和反射(一) c# API接受图片文件以Base64格式上传图片 .NET读取json数据并绑定到对象
EF+LINQ事物处理 在使用EF的情况下,怎么进行事务的处理,来减少数据操作时的失误,比如重复插入数据等等这些问题,这都是经常会遇到的一些问题 但是如果是我有多个站点,然后存在同类型的角色去操作 ...
- [转]C# 使用Nlog记录日志到数据库
本文转自:http://www.cnblogs.com/weixing/archive/2013/04/26/3044422.html 摘要]Nlog是一个很不错的.NET日志记录组件,它可以将日志输 ...
- .NET中使用NLog记录日志
以前小编记录日志使用的是Log4Net,虽然好用但和NLog比起来稍显复杂.下面小编就和大伙分享一下NLog的使用方式. 引用NLog.Config 在使用NLog之前,我们要首先添加对NLog.Co ...
- Nlog 记录日志到 sqlite
最近研究了一下Nlog这个日志框架,这里记录一下如何将日志写到sqlite中. 第一步:使用NuGet获取Nlog和Sqlite 第二步:在sqlite中创建一个database,这里我用了SQLit ...
- C# 使用Nlog记录日志到数据库 使用LogEventInfo类获取,命名空间名称、类名、方法名
原文地址:http://dotnet.9sssd.com/csbase/art/793 [摘要]Nlog是一个很不错的.NET日志记录组件,它可以将日志输出到控件台,保存到文本,也可以很方便的记录到数 ...
- spring boot 入门操作(二)
spring boot入门操作 使用FastJson解析json数据 pom dependencies里添加fastjson依赖 <dependency> <groupId>c ...
- spring boot 入门操作(三)
spring boot入门操作 devtools热部署 pom dependencies里添加依赖 <dependency> <groupId>org.springframew ...
- Mysql的二进制安装和基础入门操作
前言:Mysql数据库,知识非常的多,要想学精学通这块知识,估计也要花费和学linux一样的精力和时间.小编也是只会些毛皮,给大家分享一下~ 一.MySQL安装 (1)安装方式: 1 .程序包yum安 ...
- java之servlet入门操作教程一续
本节主要是在java之servlet入门操作教程一 的基础上使用myeclipse实现自动部署的功能 准备: java之servlet入门操作教程一 中完成myFirstServlet项目的创建: ...
随机推荐
- 018 spark on yarn (Job history)的配置,主要是yarn处跳转到历史聚合页面
一:目标 1.目标 在yarn的8080页面可以跳转到spark的日志18080页面. 因为在运行spark之后,看对应的job的日志,这样直接连接,更合理直接. 2.总结 在后面可以看到,其实不需要 ...
- 如何查看Ubuntu版本,以及Linux内核版本??
查看Ubuntu版本: 方法一: cat /etc/issue 方法二: sudo lsb_release -a 查看内核版本: uname -r
- HDU - 1712 - ACboy needs your help 【分组背包】
<题目链接> 题目大意:有n个课程,现在花M天来学习这些课程,学习每个课程花的天数所得到的价值不同,求M天怎么分配学习才能得到的价值最大.(这些课程得到的价值和所花天数的关系由矩阵给出) ...
- rdesktop方法(Linux to Windows)
我的配置: rdesktop -g 960x1080 -a 16 -u aura-bd -0 192.168.62.241 1. 准备工作: ubuntu端: sudo apt-get install ...
- python模块——PrettyTable
python模块——PrettyTable 一. 简介 Python通过prettytable模块将输出内容如表格方式整齐输出,可用来生成美观的ASCII格式的表格,十分实用. python本身并不内 ...
- JavaNIO快速入门
NIO是Jdk中非常重要的一个组成部分,基于它的Netty开源框架可以很方便的开发高性能.高可靠性的网络服务器和客户端程序.本文将就其核心基础类型Channel, Buffer, Selector进行 ...
- ios技术篇:导航栏push遵循的三个规则
1.如果B视图有一个自定义的左侧按钮(leftBarButtonItem),则会显示这个自定义按钮 2.如果B没有自定义按钮,但是A视图的backBarButtonItem属性有自定义项,则显示这个自 ...
- 数据操作流DataOutputStream、DataInputStream类
[例子1] import java.io.DataOutputStream; import java.io.File; import java.io.FileOutputStream; import ...
- [ 转载 ]Java:成员变量,局部变量,静态变量的区别
精简后方便自己理解. 成员变量 我们研究一个事物: 属性:外在特征:如身高,体重 行为:能做什么:如说话,打球. 在Java语言中,最基本的单位是类(class),类就是用来体现事物的. 属性:类中的 ...
- Python3Numpy——相关性协方差应用
基本理论 Correlation Are there correlations between variables? Correlation measures the strength of the ...