Created
January 11, 2022 13:37
-
-
Save ruyut/dc97b020aee9da5b2f7d01df9bac0ee5 to your computer and use it in GitHub Desktop.
C# NLog示範
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using NLog; | |
using NLog.Config; | |
using NLog.Targets; | |
namespace RuyutConsoleApp | |
{ | |
internal class Program | |
{ | |
private static void Main(string[] args) | |
{ | |
CreateLogger(); | |
Logger logger = LogManager.GetCurrentClassLogger(); | |
logger.Trace("Trace"); | |
logger.Debug("Debug"); | |
logger.Info("Info"); | |
logger.Warn("Warn"); | |
logger.Error("Error"); | |
logger.Fatal("Fatal"); | |
Console.ReadLine(); | |
} | |
private static void CreateLogger() | |
{ | |
var config = new LoggingConfiguration(); | |
var fileTarget = new FileTarget | |
{ | |
FileName = "${basedir}/logs/${shortdate}.log", | |
Layout = "${date:format=yyyy-MM-dd HH\\:mm\\:ss} [${uppercase:${level}}] ${message}", | |
}; | |
config.AddRule(LogLevel.Trace, LogLevel.Fatal, fileTarget); | |
LogManager.Configuration = config; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment