.Net Core Mvc中使用Nlog

安装Nuget包

NLog.Web.AspNetCore

新建配置文件 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"
 autoReload="true"
 internalLogLevel="Warn"
 internalLogFile="internal-nlog.txt">

 <!--define various log targets-->
 <targets>

 <!--write logs to file-->
 <target xsi:type="File" name="allfile" fileName="Logs/nlog-all-${shortdate}.log"
 layout="${longdate}|${logger}|${uppercase:${level}}|${message} ${exception}" />

 <target xsi:type="File" name="ownFile-web" fileName="Logs/nlog-my-${shortdate}.log"
 layout="${longdate}|${logger}|${uppercase:${level}}|${message} ${exception}" />

 <target xsi:type="Null" name="blackhole" />

 </targets>

 <rules>
 <!--All logs, including from Microsoft-->
 <logger name="*" minlevel="Trace" writeTo="allfile" />

 <!--Skip Microsoft logs and so log only own logs-->
 <logger name="Microsoft.*" minlevel="Trace" writeTo="blackhole" final="true" />
 <logger name="*" minlevel="Trace" writeTo="ownFile-web" />
 </rules>


</nlog>

Startup.csConfigure方法中添加

            loggerFactory.AddNLog();//添加NLog
            env.ConfigureNLog("nlog.config");//读取Nlog配置文件

使用例子

        static Logger Logger = LogManager.GetCurrentClassLogger();
        public IActionResult Index()
        {
            Logger.Info("普通信息日志-----------");
            Logger.Debug("调试日志-----------");
            Logger.Error("错误日志-----------");
            Logger.Fatal("异常日志-----------");
            Logger.Warn("警告日志-----------");
            Logger.Trace("跟踪日志-----------");
            Logger.Log(NLog.LogLevel.Warn, "Log日志------------------");

            return View();
        }

然后运行站点,会在bin目录下有个Logs文件夹里面就是记录的日志
nlog-all开头的是整个站点Nlog自动记录的日志
nlog-my是代码输出的日志

暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇