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. mysql更新字段值提示You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column To disable safe mode

    1 引言 当更新字段缺少where语句时,mysql会提示一下错误代码: Error Code: 1175. You are using safe update mode and you tried ...

  2. 微信支付之JsApi支付

    常见问题:金额错误,微信金额是int类型,最小单位为分,即是1 客户端调用微信支付的时候一闪而过:这个原因是因为微信商户后台支付目录地址没设置对,导致js调用的时候验证没通过 .aspx页面设置: x ...

  3. node 相关网站

    包管理网站:https://www.npmjs.com/

  4. CentOS切换为iptables防火墙并进行相关配置

    CentOS切换为iptables防火墙 切换到iptables首先应该关掉默认的firewalld,然后安装iptables服务. 1.关闭firewall: service firewalld s ...

  5. vmware+CentOs 6.9的安装步骤

    一.安装步骤 linux分区 登录用户名和密码 登录用户名和密码后安装成功 二.远程控制Xshell的安装

  6. Linux在bash history当中添加timestamp

    执行以下两条命令即可 echo 'export HISTTIMEFORMAT="%y-%m-%d %T "' >> /etc/profile source /etc/p ...

  7. LeetCode(1):两数之和

    写在前面:基本全部参考大神“Grandyang”的博客,附上网址:http://www.cnblogs.com/grandyang/p/4130379.html 写在这里,是为了做笔记,同时加深理解, ...

  8. laravel使用when搜索遇到状态参数(有0的状态)的坑

    今天,使用when()方法写活动列表的接口,有两个状态搜索,都有0这个状态,当传参为0时when()就失效了... 反反复复的验证参数,传参确实是0和1啊...百思不得其解~~~后面仔细想想when( ...

  9. python 全栈开发,Day1(python介绍,变量,if,while)

    python基础一 一,Python介绍 python的出生与应用 python的创始人为吉多·范罗苏姆(Guido van Rossum).1989年的圣诞节期间,吉多·范罗苏姆(中文名字:龟叔)为 ...

  10. poj 2406 求字符串中重复子串的个数

    Sample Input abcdaaaaababab.Sample Output 1 //1个abcd4 //4个a3 //3个ab #include<stdio.h> #include ...