[研究] [ASP.NET] NLog 4.7.4 日誌套件安裝、試用 (NuGet)
2020-09-11NLog 官方網站
http://nlog-project.org/
Documentation
http://nlog-project.org/documentation/
Wiki
https://github.com/nlog/nlog/wiki
教學
https://github.com/nlog/nlog/wiki/Tutorial
CodePlex:
http://nlog.codeplex.com/
github:
https://github.com/jkowalski/NLog
NuGet Gallery:
http://nuget.org/List/Packages/NLog
環境:Visual Studio 2019
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"> <!-- optional, add some variables https://github.com/nlog/NLog/wiki/Configuration-file#variables --> <variable name="myvar" value="myvalue"/> <!-- See https://github.com/nlog/nlog/wiki/Configuration-file for information on customizing logging rules and outputs. --> <targets> <!-- add your targets here See https://github.com/nlog/NLog/wiki/Targets for possible targets. See https://github.com/nlog/NLog/wiki/Layout-Renderers for the possible layout renderers. --> <!-- Write events to a file with the date in the filename. <target xsi:type="File" name="f" fileName="${basedir}/logs/${shortdate}.log" layout="${longdate} ${uppercase:${level}} ${message}" /> --> <target xsi:type="File" name="f" fileName="${basedir}/logs/${shortdate}.log" layout="${longdate} ${uppercase:${level}} ${message}" /> </targets> <rules> <!-- add your logging rules here --> <!-- Write all events with minimal level of Debug (So Debug, Info, Warn, Error and Fatal, but not Trace) to "f" <logger name="*" minlevel="Debug" writeTo="f" /> --> <logger name="*" minlevel="Debug" writeTo="f" /> </rules> </nlog> |
Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="NLogTest2.Default" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" /> </div> </form> </body> </html> |
Default.aspx.cs
using System; namespace NLogTest2 { public partial class Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void Button1_Click(object sender, EventArgs e) { NLog.Logger Logger = NLog.LogManager.GetCurrentClassLogger(); try { Logger.Info("Hello world"); // Logger.Error("Error Message");System.Console.ReadKey(); } catch (Exception ex) { Logger.Error(ex, "Goodbye cruel world"); } } } } |
結果:輸出檔案。
(完)
相關
https://www.google.com.tw/search?q=Nlog+site%3Akevintsengtw.blogspot.com
使用NLog - Advanced .NET Logging (1)
http://kevintsengtw.blogspot.tw/2011/10/nlog-advanced-net-logging-1.html
沒有留言:
張貼留言