/// <summary> /// 使用NLog /// </summary> /// <param name="builder"></param> /// <param name="configPath">配置文件路径,默认调用当前项目执行目录下面的log4net.config作为配置文件</param> /// <returns></returns> public static IWebHostBuilder UseNLog(this IWebHostBuilder builder, string configPath, string basePath = null) { if (builder == null) { throw new ArgumentNullException(nameof(builder)); } NLogContext.Configure(configPath, basePath); return(builder); }
/// <summary> /// 设置配置文件 /// </summary> /// <param name="configuration"></param> /// <param name="configPath">配置文件路径,默认调用当前项目执行目录下面的log4net.config作为配置文件</param> /// <returns></returns> public static void Configure(string configPath) { if (string.IsNullOrEmpty(configPath)) { var currentDir = Directory.GetCurrentDirectory(); configPath = $@"{currentDir}\Config\nlog.config"; } LogFactory factory = NLog.Web.NLogBuilder.ConfigureNLog(configPath); NLogContext.Configure(factory); }
/// <summary> /// 设置配置文件 /// </summary> /// <param name="configuration"></param> /// <param name="configPath">配置文件路径,默认调用当前项目执行目录下面的log4net.config作为配置文件</param> /// <returns></returns> public static void Configure(string configPath) { FileInfo file = new FileInfo(configPath); if (file.Exists) { LogFactory factory = NLog.Web.NLogBuilder.ConfigureNLog(configPath); NLogContext.Configure(factory); if (factory.IsLoggingEnabled()) { NLogger.Info($"初始化{configPath}完成。"); } } else { throw new Exception($"未找到{file.FullName}文件"); } }
/// <summary> /// 设置配置文件 /// </summary> /// <param name="configuration"></param> /// <param name="configPath">配置文件路径,默认调用当前项目执行目录下面的log4net.config作为配置文件</param> /// <returns></returns> public static void ConfigureNLog(this IConfiguration configuration, string configPath, string basePath = null) { NLogContext.Configure(configPath, basePath); }