当你在谷歌搜索 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. Markdown初步使用

    一.兼容 HTML Markdown 的理念是,能让文档更容易读.写和随意改.HTML 是一种发布的格式,Markdown 是一种书写的格式.就这样,Markdown 的格式语法只涵盖纯文本可以涵盖的 ...

  2. 推荐PHP程序员进阶的好书

    <UNIX网络编程卷1(第3版)> <UNIX网络编程卷2(第2版)> <UNIX环境高级编程(第3版)> <UNIX编程艺术> <MySQL技术 ...

  3. Linux命令之hostname - 显示或设置主机名

    我使用过的Linux命令之hostname - 显示或设置主机名 本文链接:http://codingstandards.iteye.com/blog/804648   (转载请注明出处) 用途说明 ...

  4. node的express中使用socket.io

    服务器端server.js代码 var express=require("express"); var http=require("http"); var si ...

  5. Windows Azure 应用程序短暂性故障处理

    这两天在做一个Windows Azure blob存储备份的的一个小功能,但是每次使用CloudBlockBlob.UploadFromStream上传本地文件到Blob Storage,总是不成功报 ...

  6. python写个Hack Scan

    前言: 之前逛SAFEING极客社区的时候 发现一款黑市卖2000多的软件,后面下载了 打不开.发现config文件里面有些不错的东西.总结了一下 有了以下的脚本. 脚本用处: [1]探测CMS(不敢 ...

  7. Protobuf3教程

    Protobuf3教程 https://blog.csdn.net/hulinku/article/details/80827018 Protobuf语言指南——.proto文件语法详解 https: ...

  8. 第五章 服务容错保护: Spring Cloud Hystrix

    在微服务架构中, 存在着那么多的服务单元, 若一个单元出现故障, 就很容易因依赖关系而引发故障的蔓延,最终导致整个系统的瘫痪,这样的架构相较传统架构更加不稳定.为了解决这样的问题, 产生了断路器等一系 ...

  9. mongodb 安装、windows服务、创建用户

    http://www.cnblogs.com/best/p/6212807.html 打开MongoDB的安装目录如“C:\Program Files\MongoDB\Server\3.4\bin”, ...

  10. leetcode382

    /** * Definition for singly-linked list. * public class ListNode { * public int val; * public ListNo ...