You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

80 lines
3.1 KiB
C#

6 years ago
using System;
6 years ago
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using NLog;
using NLog.Config;
using NLog.Extensions.Logging;
6 years ago
namespace NLogStudy.CoreApp2.ConsoleApp
{
class Program
{
static void Main(string[] args)
{
6 years ago
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.configdotnet 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时
6 years ago
}
}
}