NUGET添加NLog和NLog.Config的引用

配置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"> <!--archiveAboveSize以字节为单位,1K位1024字节,1048576表示1M,104857600表示100M-->
<targets>
<target xsi:type="File" name="logfile" fileName="/data/logs/${shortdate}.log"
archiveFileName="/data/logs/${shortdate}.{#####}.txt"
archiveAboveSize="10485760"
archiveNumbering="Rolling"
concurrentWrites="true"
maxArchiveFiles="5"
keepFileOpen="false" />
</targets> <rules>
<!--DEBUG,INFO,WARN,ERROR,FATAL-->
<logger name="*" minlevel="Debug" writeTo="logfile" />
</rules>
</nlog>

添加LogHelper

    /// <summary>
/// NLog辅助类
/// 创建人:苏本东
/// 创建时间:2019/3/22 10:49:00
/// </summary>
public class LogHelper
{
private static Logger Logger = LogManager.GetCurrentClassLogger(); #region 普通级别 /// <summary>
/// 普通级别
/// </summary>
/// <param name="content"></param>
public static void Info(string content)
{
Logger.Info(content);
} /// <summary>
/// 普通级别
/// </summary>
/// <param name="exception"></param>
/// <param name="content"></param>
public static void Info(Exception exception, string content)
{
Logger.Info(exception, content);
} #endregion #region 警告级别 /// <summary>
/// 警告级别
/// </summary>
/// <param name="content"></param>
public static void Warn(string content)
{
Logger.Warn(content);
} /// <summary>
/// 警告级别
/// </summary>
/// <param name="exception"></param>
/// <param name="content"></param>
public static void Warn(Exception exception, string content)
{
Logger.Warn(exception, content);
} #endregion #region 错误级别 /// <summary>
/// 错误级别
/// </summary>
/// <param name="content"></param>
public static void Error(string content)
{
Logger.Error(content);
} /// <summary>
/// 错误级别
/// </summary>
/// <param name="exception"></param>
/// <param name="content"></param>
public static void Error(Exception exception, string content)
{
Logger.Error(exception, content);
} #endregion
}

使用NLog

    public class LogController : Controller
{
public ActionResult Index()
{
LogHelper.Info("普通信息日志");
LogHelper.Warn("警告信息日志");
LogHelper.Error("错误信息日志");
LogHelper.Info(new Exception("异常发生"), "异常信息"); return Content("success");
}
}

注意:根据我的配置,日志会输出到C盘根目录下,而不是项目根目录下,这点需要注意。

参考网址:https://www.cnblogs.com/kejie/p/6211597.html,上半部分内容。

.net framework 4.6 asp.net mvc 使用NLog的更多相关文章

  1. [转]Sorting, Filtering, and Paging with the Entity Framework in an ASP.NET MVC Application (3 of 10)

    本文转自:http://www.asp.net/mvc/overview/older-versions/getting-started-with-ef-5-using-mvc-4/sorting-fi ...

  2. ASP.NET MVC 与NLog的使用

    NLog是一个.NET 下一个完善的日志工具,个人已经在项目中使用很久,与ELMAH相比,可能EAMAH更侧重 APS.NET MVC 包括调试路由,性能等方面,而NLog则更简洁. github: ...

  3. MVC中使用EF(1):为ASP.NET MVC程序创建Entity Framework数据模型

    为ASP.NET MVC程序创建Entity Framework数据模型 (1 of 10) By  Tom Dykstra |July 30, 2013 Translated by litdwg   ...

  4. ASP.NET MVC随想录——锋利的KATANA

    正如上篇文章所述那样,OWIN在Web Server与Web Application之间定义了一套规范(Specs),意在解耦Web Server与Web Application,从而推进跨平台的实现 ...

  5. 连接弹性和命令拦截的 ASP.NET MVC 应用程序中的实体框架

    最近悟出来一个道理,在这儿分享给大家:学历代表你的过去,能力代表你的现在,学习代表你的将来. 十年河东十年河西,莫欺少年穷 学无止境,精益求精    上篇博客我们学习了EF 之 MVC 排序,查询,分 ...

  6. [渣译文] 使用 MVC 5 的 EF6 Code First 入门 系列:为ASP.NET MVC应用程序读取相关数据

    这是微软官方教程Getting Started with Entity Framework 6 Code First using MVC 5 系列的翻译,这里是第七篇:为ASP.NET MVC应用程序 ...

  7. [渣译文] 使用 MVC 5 的 EF6 Code First 入门 系列:为ASP.NET MVC应用程序更新相关数据

    这是微软官方教程Getting Started with Entity Framework 6 Code First using MVC 5 系列的翻译,这里是第八篇:为ASP.NET MVC应用程序 ...

  8. [渣译文] 使用 MVC 5 的 EF6 Code First 入门 系列:为ASP.NET MVC应用程序使用异步及存储过程

    这是微软官方教程Getting Started with Entity Framework 6 Code First using MVC 5 系列的翻译,这里是第九篇:为ASP.NET MVC应用程序 ...

  9. 压测 linux + jexus + mono + asp.net mvc

    环境: 1.centos 7 + jexus 5.8.1 + mono 4.4.2 + asp.net mvc 4 做了一点小优化: 一.调整文件描述符数量限制编辑 /etc/security/lim ...

随机推荐

  1. Expm 1_3 数组中逆序对个数问题

    有一个数的序列A[1].A[2] .A[3] .…… .A[n],若i<j,并且A[i]>A[j],则称A[i]与A[j]构成了一个逆序对,设计算法求数列A中逆序对的个数. package ...

  2. nodejs后台向后台get请求

    1 前言 有时在nodejs写的服务端某方法需要向服务端另一个接口发送get请求,可以使用第三方库,然后直接使用即可,此文章只是用来记录使用 2 方法 2.1 get 请求 //1. Install ...

  3. 图解 Paxos 一致性协议

    转自:http://blog.jobbole.com/106327/ 前言 Paxos 一致性协议可以说是一致性协议研究的起点,也以难以理解闻名.其实协议本身并没有多难理解,它的难理解性主要体现在:为 ...

  4. Android ImageView 的scaleType 属性图解

    ImageView 是 Android 中最常用的控件之一,而在使用ImageView时,必不可少的会使用到它的scaleType属性.该属性指定了你想让ImageView如何显示图片,包括是否进行缩 ...

  5. vue scoped 穿透_vue 修改内部组件样式问题

    何为scoped? 在vue文件中的style标签上,有一个特殊的属性:scoped.当一个style标签拥有scoped属性时,它的CSS样式就只能作用于当前的组件,也就是说,该样式只能适用于当前组 ...

  6. java多线程快速入门(十)

    synchonizd解决安全性问题 package com.cppdy; class MyThread6 implements Runnable{ private Integer ticketCoun ...

  7. poj2185 kmp求最小覆盖矩阵,好题!

    /* 特征值k=m-next[m]就是最小循环节的长度, m%k就是去末尾遗留长度 */ #include<iostream> #include<cstring> #inclu ...

  8. poj1179 环形+区间dp

    因为要用到模,所以左起点设置为0比较好 #include<iostream> #include<cstdio> #include<cstring> #define ...

  9. python+selenium五:多窗口切换与获取句柄

    from selenium import webdriverfrom selenium.webdriver.common.by import Byimport time driver = webdri ...

  10. layer.js弹出框

    HTML <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF- ...