当你在谷歌搜索 Application  Loggin in C#,排在最前面的是这个 .NET Logging Tools and Libraries ,点击进去你会发现里面收录了不错的日记工具及文章。

这里我要介绍的是NLog。log4net 也是一个不错的选择。

NLog 官网:http://nlog-project.org/

NLog Github: https://github.com/NLog/NLog/wiki/Tutorial

先介绍使用方法:

1.安装

用Nuget 安装 NLog.Config。

Install-Package NLog.Config

这样他就会把需要的包都安装引入进到所安装的项目了。

2.配置

配置日记记录的形式,存储的地方等。安装好NLog之后,会自动生成一个NLog.config

打开它,里面其实会有详细的Demo教你怎么配置。

首先<target>, target 说明日志存放的地方。

    <!--
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 xsi:type="File" name="debugfile" fileName="${basedir}/logs/${shortdate}.debug.html"
layout="Date: ${longdate} Logger: ${logger} Level: ${uppercase:${level}} &lt;BR&gt; Message: ${message} &lt;BR&gt;&lt;HR Size=1&gt;"/>
<target xsi:type="File" name="errorfile" fileName="${basedir}/logs/${shortdate}.error.html"
layout="Date: ${longdate} Logger: ${logger} Level: ${uppercase:${level}} &lt;BR&gt; Message: ${message} &lt;BR&gt;&lt;HR Size=1&gt;" />

其次是定义规则,日记也分为几个级别(Debug,Info,Warn,Error,Fatal 从左到右级别增加),不同的日记可以分开管理记录。

  <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" maxlevel="Debug" writeTo="debugfile" />
<logger name="*" minlevel="Info" writeTo="errorfile"/> </rules>

最后就是调用了。

using System.Web.Mvc;
using NLog; namespace App.Controllers
{
public class BaseController : Controller
{
protected Logger Log { get; private set; } protected BaseController()
{
Log = LogManager.GetLogger(GetType().FullName);
}
}
}
using System;

namespace App.Controllers
{
public class UserController : BaseController
{
public void Index()
{
Log.Debug("This is Debug");
Log.Error(new Exception("除数不能为零"));
Log.Info("使用起来简单吧");
}
}
}

出来的效果:

2016-06-13.debug.html

Date: -- ::17.5376 Logger: App.Controllers.UserController Level: DEBUG <BR> Message: This is Debug <BR><HR Size=>

2016-06-13.error.html

Date: -- ::17.5486 Logger: App.Controllers.UserController Level: ERROR <BR> Message: System.Exception: 除数不能为零 <BR><HR Size=>
Date: -- ::17.5486 Logger: App.Controllers.UserController Level: INFO <BR> Message: 使用起来简单吧 <BR><HR Size=>

上面target 只有 file。其实还有很多种形式的输出,这个要在下一篇再写了。

Nlog- Application Logging in C#的更多相关文章

  1. qt application logging

    “AnalysisPtsDataTool201905.exe”(Win32): 已加载“F:\OpencvProject\ZY-Project\x64\Debug\AnalysisPtsDataToo ...

  2. [转]ASP.NET Core 开发-Logging 使用NLog 写日志文件

    本文转自:http://www.cnblogs.com/Leo_wl/p/5561812.html ASP.NET Core 开发-Logging 使用NLog 写日志文件. NLog 可以适用于 . ...

  3. Core 开发-Logging 使用NLog

    ASP.NET Core 开发-Logging 使用NLog 写日志文件   ASP.NET Core 开发-Logging 使用NLog 写日志文件. NLog 可以适用于 .NET Core 和 ...

  4. ASP.NET Core 开发-Logging 使用NLog 写日志文件

    ASP.NET Core 开发-Logging 使用NLog 写日志文件. NLog 可以适用于 .NET Core 和 ASP.NET Core . ASP.NET Core已经内置了日志支持,可以 ...

  5. NLog在.NET Core Console Apps中的简单应用

    什么是NLog? NLog is a free logging platform for .NET with rich log routing and management capabilities. ...

  6. Nlog 配置总结

    Writes log messages to one or more files. Since NLog 4.3 the ${basedir} isn't needed anymore for rel ...

  7. ASP.NET Core使用NLog记录日志到Microsoft Sql Server

    在之前的文章中介绍了如何在ASP.NET Core使用NLog,本文为您介绍在ASP.NET Core使用NLog记录到Microsoft Sql Server 1.我们需要添加依赖: NLog.We ...

  8. .Net Core 使用NLog记录日志到文件和数据库

    NLog 记录日志是微软官方推荐使用. 接下来,通过配置日志记录到文件和Sql Server数据库. 第一步:首先添加包NLog.Config (可通过微软添加包命令Install-Package 包 ...

  9. [转]Setting the NLog database connection string in the ASP.NET Core appsettings.json

    本文转自:https://damienbod.com/2016/09/22/setting-the-nlog-database-connection-string-in-the-asp-net-cor ...

  10. Logging configuration

    The Play logger is built on Log4j. Since most Java libraries use Log4j or a wrapper able to use Log4 ...

随机推荐

  1. (转)Inno Setup入门(十三)——Pascal脚本(2)

    本文转载自:http://blog.csdn.net/yushanddddfenghailin/article/details/17250933 事件函数(2) function CheckPassw ...

  2. flv格式详解+实例剖析

    简介 FLV(Flash Video)是现在非常流行的流媒体格式,由于其视频文件体积轻巧.封装播放简单等特点,使其很适合在网络上进行应用,目前主流的视频网站无一例外地使用了FLV格式.另外由于当前浏览 ...

  3. spring扩展点之一:BeanFactoryPostProcessor和BeanPostProcessor

    一.BeanFactoryPostProcessor和BeanPostProcessor的区别 BeanFactoryPostProcessor和BeanPostProcessor都是spring初始 ...

  4. 准确计算Java中对象的大小

    由于在项目中需要大致计算一下对象的内存占用率(Hadoop中的Reduce端内存占用居高不下却又无法解释),因此深入学习了一下如何准确计算对象的大小. 使用system.gc()和java.lang. ...

  5. 004:MySQL数据库体系结构

    目录 一. MySQL数据库体系结构 1.MySQL数据库体系结构介绍 1 数据库定义 2 数据库实例 2. MySQL体系结构 1 单进程多线程结构 2 存储引擎的概念 3 体系结构图 4 逻辑存储 ...

  6. Centos 文件查找命令

    find [搜索范围] [搜索条件] #搜索文件 find / -name install.log #避免大范围搜索,会非常耗费系统资源 #find是在系统当中搜索符合条件的文件名.如果需要匹配,使用 ...

  7. 上传图片demo

    页面: js: 后台: @RequiresPermissions("pointwall:upload:edit") @RequestMapping(value = "sa ...

  8. Linux 之不同运维人员共用root 账户权限审计

    一.为什么? 在中小型企业,公司不同运维人员基本都是以root 账户进行服务器的登陆管理,缺少了账户权限审计制度.不出问题还好,出了问题,就很难找出源头. 这里介绍下,如何利用编译bash 使不同的客 ...

  9. linux中sed命令

    sed sed意为流编辑器(Stream Editor),在Shell脚本和Makefile中作为过滤器使用非常普遍,也就是把前一个程序的输出引入sed的输入,经过一系列编辑命令转换为另一种格式输出. ...

  10. URL里面的中文参数到底转换成了什么?

    https://www.amazon.cn/s/ref=nb_sb_noss?__mk_zh_CN=%E4%BA%9A%E9%A9%AC%E9%80%8A%E7%BD%91%E7%AB%99& ...