|
|
using System;
|
|
|
using Microsoft.Extensions.Configuration;
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
using NLog;
|
|
|
using NLog.Config;
|
|
|
using NLog.Extensions.Logging;
|
|
|
|
|
|
namespace NLogStudy.CoreApp2.ConsoleApp
|
|
|
{
|
|
|
class Program
|
|
|
{
|
|
|
static void Main(string[] args)
|
|
|
{
|
|
|
var logger = LogManager.GetCurrentClassLogger();
|
|
|
|
|
|
var logger2 = LogManager.GetLogger(typeof(Program).FullName);
|
|
|
logger2.Error("========{demo}-{test}",021,88888888);
|
|
|
try
|
|
|
{
|
|
|
logger.Error("--------");
|
|
|
}
|
|
|
catch (Exception ex)
|
|
|
{
|
|
|
// NLog: catch any exception and log it.
|
|
|
logger.Error(ex, "Stopped program because of exception");
|
|
|
throw;
|
|
|
}
|
|
|
finally
|
|
|
{
|
|
|
// Ensure to flush and stop internal timers/threads before application-exit (Avoid segmentation fault on Linux)
|
|
|
LogManager.Shutdown();
|
|
|
}
|
|
|
|
|
|
Console.ReadKey();
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 显式(手动)加载配置文件
|
|
|
/// </summary>
|
|
|
static void ExplicitLoadingConfig()
|
|
|
{
|
|
|
//加载配置文件:指定Nlog.config
|
|
|
NLog.LogManager.Configuration = new NLog.Config.XmlLoggingConfiguration("nlog.config");
|
|
|
|
|
|
//加载配置文件:从指定字符串
|
|
|
var xmlStream = new System.IO.StringReader("<nlog>*****</nlog>");
|
|
|
var xmlReader = System.Xml.XmlReader.Create(xmlStream);
|
|
|
NLog.LogManager.Configuration = new NLog.Config.XmlLoggingConfiguration(xmlReader, null);
|
|
|
|
|
|
//加载配置文件: Xamarin 资源文件
|
|
|
|
|
|
//加载配置文件: Xamarin Android
|
|
|
//Xamarin Android,将自动扫描 assets文件夹中的 NLog.config
|
|
|
LogManager.Configuration = new XmlLoggingConfiguration("assets/someothername.config");
|
|
|
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 自动加载配置文件
|
|
|
/// 在启动时,NLog会在各种文件中搜索其配置,如下所述。
|
|
|
/// 它加载找到的第一个nlog配置:找到第一个nlog配置后,搜索结束;如果找不到配置,则NLog失败。
|
|
|
/// </summary>
|
|
|
static void AutoLoadingConfig()
|
|
|
{
|
|
|
//对于独立的*.exe应用程序,按如下顺序搜索配置文件
|
|
|
//标准应用程序配置文件(通常为applicationname.exe.config)
|
|
|
//应用程序目录中的applicationname.exe.nlog
|
|
|
//应用程序目录中的NLog.config(dotnet core应用,运行在docker中时,配置文件名大小写敏感,即区分大写小)
|
|
|
//NLog.dll所在目录中的NLog.dll.nlog(仅当GAC中未安装NLog时)
|
|
|
|
|
|
//对于ASP.NET、ASP.NET Core 应用程序,按如下顺序搜索配置文件
|
|
|
//标准Web应用程序文件web.config
|
|
|
//web.nlog与web.config位于同一目录
|
|
|
//应用程序目录中的NLog.config
|
|
|
//NLog.dll所在目录中的NLog.dll.nlog(仅当GAC中未安装NLog时)
|
|
|
}
|
|
|
}
|
|
|
}
|