Skip to content

Instantly share code, notes, and snippets.

@ruyut
Created January 11, 2022 13:37
Embed
What would you like to do?
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