public static void Main(string[] args) { ConfigNlog configNlog = new ConfigNlog(Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT")); LogManager.Configuration = configNlog.GetLogConfig(); var logger = LogManager.GetCurrentClassLogger(); try { logger.Debug("Application Starting Up"); CreateHostBuilder(args).Build().Run(); } catch (Exception exception) { logger.Error(exception, "Stopped program because of exception"); throw; } finally { NLog.LogManager.Shutdown(); } }
public void TestNLogger() { ConfigNlog configNlog = new ConfigNlog("Development"); LogManager.Configuration = configNlog.GetLogConfig(); var logger = LogManager.GetCurrentClassLogger(); Guid guid = Guid.NewGuid(); logger.Error(guid.ToString()); FileTarget target = (FileTarget)logger.Factory.Configuration.FindTargetByName(LogFileDev.FILENAME); string[] lines = System.IO.File.ReadAllLines(target.FileName.Render(new LogEventInfo()).Replace(@"/", @"\")); bool found = false; foreach (string line in lines) { if (line.Contains(guid.ToString())) { found = true; break; } } Assert.IsTrue(found); }