ASP.Net Logging Using NLog
来源:互联网 发布:数控编程指令代码 编辑:程序博客网 时间:2024/05/16 23:40
I should have been logging information and errors from day 1, but I guess its never too late to start. Based on Rob Conery's "endorsement", I chose to try NLog.
Step 1 was obviously to download the library. Then, I added the reference to NLog.dll in my web project and configured it. For configuration, NLog uses its own configuration file separate from Web.config and you don't have to do any modifications to Web.config at all. Below is my NLog.config, which is set up to log everything of level Info and above to the file specified (NLog.config goes in the root of the web app). As you can see, it is very straightforward to configure for your own needs:
<?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" > <targets> <target name="console" xsi:type="ColoredConsole" layout="${date:format=HH\:mm\:ss}|${level}|${stacktrace}|${message}" /> <target name="file" xsi:type="File" fileName="${basedir}/Logs/rrLog.log" layout="${date}: ${message}" /> </targets> <rules> <logger name="*" minlevel="Info" writeTo="file" /> </rules></nlog>
After this, all that is left is to actually put in your log statements. So for example, to log all unhandled exceptions in your ASP.Net application, put the following in Global.asax.cs:
protected void Application_Error() { Exception lastException = Server.GetLastError(); NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger(); logger.Fatal(lastException);}
And that's it! It can't be any easier than that, so I am pretty happy with NLog right now. I might checkout Log4Net sometime to make a comparison, but I don't have any reason to do so at this point. BTW, I recommend creating your own Logger service that wraps NLog so that you can easily switch to a different library if needed. I'll be doing that right now.
- ASP.Net Logging Using NLog
- ASP.NET Core 开发-Logging 使用NLog 写日志文件
- NLog - a .NET logging tool
- .net core日志 NLog.Extensions.Logging
- ASP.NET Core 和 NLog 集成
- NLog 在winform和asp.net下使用快速攻略
- NLog 在winform和asp.net下使用快速攻略
- NLog 在winform和asp.net下使用快速攻略 .
- .NET NLog 详解(一)
- .NET NLog 详解(二)
- Diagnostics and Logging In ASP.NET
- asp.net core源码飘香:Logging组件
- asp.net core源码飘香:Logging组件
- Using Cookies in ASP.NET
- ASP.NET之Using用法
- Using Plupload in ASP.NET
- NLog 在Asp.NET MVC5中使用文件和数据库设置
- HTMLContentParser ASP.NET Project using VB.NET
- getch()写入和scanf问题
- 如何在BCB中调试Active Form
- 监听浏览器关闭按钮点击事件
- [C++] 关于函数的默认实参
- IBinder对象在进程间传递的形式(一)
- ASP.Net Logging Using NLog
- Tab-Menu
- android 解析json数据格式
- Android APK的签名与重新签名
- 各种编码的由来(很通俗)
- 获取用户密码
- Struts 2.x 与 struts 1.x 比较
- Measuring Element Dimension and Location with CSSOM in Internet Explorer 9
- http://www.unc.edu/~shubin/