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. 在docker中部署centos7镜像

    本篇文章参考自: https://www.cnblogs.com/linjj/p/5606911.html https://blog.csdn.net/u012767761/article/detai ...

  2. 002_mtr_a network diagnostic tool

    一. mtr combines the functionality of the traceroute and ping programs in a single network diagnostic ...

  3. Laravel框架中Blade模板的用法

    1. 继承.片段.占位.组件.插槽 1.1 继承 1.定义父模板 Laravel/resources/views/base.blade.php 2.子模板继承 @extends('base') 1.2 ...

  4. hdu2586 lca倍增法

    倍增法加了边的权值,bfs的时候顺便把每个点深度求出来即可 #include<iostream> #include<cstring> #include<cstdio> ...

  5. hdu 2112 map+Dijkstra

    无向图 用map 起点和终点可能一样 数组不能开太大 WA了好多发 Sample Input6xiasha westlake //起点 终点xiasha station 60xiasha Shoppi ...

  6. simple简单消息队列

    一:介绍 1.优缺点 简单,但是耦合性较高. 这种模式是生产者与消费者一一对应,就是一个产生者,有一个消费者来消费. 如果,多个消费者想消费一个队列中的消息就不适合了.这种情况在后面会接着介绍. 2. ...

  7. 096实战 在windows下新建maven项目

    1.拷贝settings到.m2文件下 2.修改文件 3.新建Project项目 4.转换为maven项目 config下转换 5.拷贝pom文件 <project xmlns="ht ...

  8. HDU 2795 Billboard【点修改】

    <题目链接> 题目大意: 有一块h*w的矩形广告板,要往上面贴广告,然后给n个1*wi的广告,要求把广告贴上去,而且要求广告要尽量往上贴并且尽量靠左, 求第n个广告的所在的位置,不能贴则为 ...

  9. 51Nod-1006【LCS】+【输出路径】模板题

    题目链接:https://vjudge.net/contest/225715#problem/B 转载于>>> 题目大意: 给出两个序列,要求输出它们的最长公共子序列. 解题思路: ...

  10. 洛谷 p1164 小A点菜 【dp(好题)】 || 【DFS】 【恰好完全装满】

    题目链接:https://www.luogu.org/problemnew/show/P1164 题目背景 uim神犇拿到了uoi的ra(镭牌)后,立刻拉着基友小A到了一家……餐馆,很低端的那种. u ...