Skip to content

Instantly share code, notes, and snippets.

@ruyut
Created January 11, 2022 13:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ruyut/dc97b020aee9da5b2f7d01df9bac0ee5 to your computer and use it in GitHub Desktop.
Save ruyut/dc97b020aee9da5b2f7d01df9bac0ee5 to your computer and use it in GitHub Desktop.
C# NLog示範
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