public void EvaluateTest() { var logEventInfo = LogEventInfo.CreateNullEvent(); logEventInfo.Level = LogLevel.Warn; Assert.Equal("Warn", SimpleLayout.Evaluate("${level}", logEventInfo)); }
public ActionResult Log(string file) { if (LogManager.Configuration == null) { throw new InvalidOperationException("Logger was not found"); } var logEntries = LogManager.Configuration.AllTargets.Cast <FileTarget>() .Select(f => { var pathInQuotes = SimpleLayout.Evaluate(f.FileName.ToString()); var path = pathInQuotes.Substring(1, pathInQuotes.Length - 2); return(new { f.Name, Path = path }); }).ToList(); if (logEntries.Count == 0) { throw new InvalidOperationException("Log entries was not found"); } var active = string.IsNullOrEmpty(file) || logEntries.All(l => l.Name != file) ? logEntries.First().Name : file; return(View(new LogModel { Active = active, LogEntries = logEntries.Select(l => l.Name), Content = ReadLog(logEntries.Single(l => l.Name.Equals(active)).Path) })); }
public void ExportLog(string savepath) { LoggingConfiguration config = Loggers.TechLogger.Factory.Configuration; FileTarget standardTarget = config.FindTargetByName("TechLoggerFile") as FileTarget; var filename = standardTarget.FileName as SimpleLayout; var logfilepath = SimpleLayout.Evaluate(filename.Text); string logdir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, Path.GetDirectoryName(logfilepath)); //File.Copy(sourcefilepath, filepath, true); using (FileStream sfs = new FileStream(savepath, FileMode.Create)) { using (ZipFile zipFile = new ZipFile()) { foreach (string logFile in Directory.GetFiles(logdir, "Application*.log", SearchOption.AllDirectories)) { using (var fs = new FileStream(logFile, FileMode.Open)) { zipFile.AddEntry(Path.GetFileName(logFile), ReadFully(fs)); } } zipFile.Save(sfs); } } }
private void ParseIncludeElement(ILoggingConfigurationElement includeElement, string baseDirectory, bool autoReloadDefault) { includeElement.AssertName("include"); string newFileName = includeElement.GetRequiredValue("file", "nlog"); var ignoreErrors = includeElement.GetOptionalBooleanValue("ignoreErrors", false); try { newFileName = ExpandSimpleVariables(newFileName); newFileName = SimpleLayout.Evaluate(newFileName); var fullNewFileName = newFileName; if (baseDirectory != null) { fullNewFileName = Path.Combine(baseDirectory, newFileName); } if (File.Exists(fullNewFileName)) { InternalLogger.Debug("Including file '{0}'", fullNewFileName); ConfigureFromFile(fullNewFileName, autoReloadDefault); } else { //is mask? if (newFileName.Contains("*")) { ConfigureFromFilesByMask(baseDirectory, newFileName, autoReloadDefault); } else { if (ignoreErrors) { //quick stop for performances InternalLogger.Debug("Skipping included file '{0}' as it can't be found", fullNewFileName); return; } throw new FileNotFoundException("Included file not found: " + fullNewFileName); } } } catch (Exception exception) { if (exception.MustBeRethrownImmediately()) { throw; } var configurationException = new NLogConfigurationException(exception, "Error when including '{0}'.", newFileName); InternalLogger.Error(exception, configurationException.Message); if (!ignoreErrors) { throw configurationException; } } }
private void Open(OpenType type) { try { var config = LogManager.Configuration; if (config == null) { return; } var target = config .ConfiguredNamedTargets .OfType <AsyncTargetWrapper>() .Where(f => f.WrappedTarget is FileTarget) .Select(f => f.WrappedTarget as FileTarget) .FirstOrDefault(); if (target == null) { return; } var filename = SimpleLayout.Evaluate(target.FileName.ToString()); if (!string.IsNullOrEmpty(filename)) { filename = filename.Replace("'", string.Empty); switch (type) { case OpenType.LogFolder: var folder = Path.GetDirectoryName(filename); _logger.Info($"Opening log folder {folder}.."); if (folder != null) { Process.Start(folder); } return; case OpenType.LogFile: _logger.Info($"Opening log file {filename}.."); Process.Start(filename); return; default: throw new Exception("Open Type not found"); } } } catch (Exception exception) { _logger.Error($"Open command for {type} failed.."); _logger.Error(exception.ToString()); } }
private void ParseIncludeElement(NLogXmlElement includeElement, string baseDirectory, bool autoReloadDefault) { includeElement.AssertName("include"); string newFileName = includeElement.GetRequiredAttribute("file"); try { newFileName = this.ExpandSimpleVariables(newFileName); newFileName = SimpleLayout.Evaluate(newFileName); if (baseDirectory != null) { newFileName = Path.Combine(baseDirectory, newFileName); } #if SILVERLIGHT newFileName = newFileName.Replace("\\", "/"); if (Application.GetResourceStream(new Uri(newFileName, UriKind.Relative)) != null) #else if (File.Exists(newFileName)) #endif { InternalLogger.Debug("Including file '{0}'", newFileName); this.ConfigureFromFile(newFileName, autoReloadDefault); } else { throw new FileNotFoundException("Included file not found: " + newFileName); } } catch (Exception exception) { InternalLogger.Error(exception, "Error when including '{0}'.", newFileName); if (exception.MustBeRethrown()) { throw; } if (includeElement.GetOptionalBooleanAttribute("ignoreErrors", false)) { return; } throw new NLogConfigurationException("Error when including: " + newFileName, exception); } }
/// <summary> /// Creates the error message and displays it. /// </summary> /// <param name="title"></param> /// <param name="e"></param> /// <returns></returns> private static DialogResult ShowThreadExceptionDialog(string title, Exception e) { //string logFilename = LogManager.Configuration.AllTargets[0]. LoggingConfiguration config = LogManager.Configuration; FileTarget standardTarget = config.FindTargetByName("logfile") as FileTarget; string logFilename = string.Empty; if (standardTarget != null) { logFilename = SimpleLayout.Evaluate(standardTarget.FileName.ToString()); } string errorMsg = string.Format( "An application error occurred.\nDescription: {0}, {1}\n\nFull details are available in the log file: {2}\n\nPlease contact the author at {3}.", e.GetType().Name, e.Message, logFilename, "http://code.google.com/p/practicesharp/issues/list"); return(MessageBox.Show(errorMsg, title, MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Stop)); }
protected override string GetFormattedMessage(LogEventInfo logEvent) { string msg = logEvent.Message + " ${exception:format=Message,Type,ToString,StackTrace}"; msg = SimpleLayout.Evaluate(msg, logEvent); LogEventInfo updatedInfo; if (msg == logEvent.Message) { updatedInfo = logEvent; } else { updatedInfo = new LogEventInfo( logEvent.Level, logEvent.LoggerName, logEvent.FormatProvider, msg, logEvent.Parameters, logEvent.Exception); } return(base.GetFormattedMessage(updatedInfo)); }
public void EvaluateTest2() { Assert.Equal("Off", SimpleLayout.Evaluate("${level}")); Assert.Equal(string.Empty, SimpleLayout.Evaluate("${message}")); Assert.Equal(string.Empty, SimpleLayout.Evaluate("${logger}")); }
private void ParseIncludeElement(ILoggingConfigurationElement includeElement, string baseDirectory, bool autoReloadDefault) { includeElement.AssertName("include"); string newFileName = includeElement.GetRequiredValue("file", "nlog"); var ignoreErrors = includeElement.GetOptionalBooleanValue("ignoreErrors", false); try { newFileName = ExpandSimpleVariables(newFileName); newFileName = SimpleLayout.Evaluate(newFileName); var fullNewFileName = newFileName; if (baseDirectory != null) { fullNewFileName = Path.Combine(baseDirectory, newFileName); } #if SILVERLIGHT && !WINDOWS_PHONE newFileName = newFileName.Replace("\\", "/"); if (Application.GetResourceStream(new Uri(fullNewFileName, UriKind.Relative)) != null) #else if (File.Exists(fullNewFileName)) #endif { InternalLogger.Debug("Including file '{0}'", fullNewFileName); ConfigureFromFile(fullNewFileName, autoReloadDefault); } else { //is mask? if (newFileName.Contains("*")) { ConfigureFromFilesByMask(baseDirectory, newFileName, autoReloadDefault); } else { if (ignoreErrors) { //quick stop for performances InternalLogger.Debug("Skipping included file '{0}' as it can't be found", fullNewFileName); return; } throw new FileNotFoundException("Included file not found: " + fullNewFileName); } } } catch (Exception exception) { InternalLogger.Error(exception, "Error when including '{0}'.", newFileName); if (ignoreErrors) { return; } if (exception.MustBeRethrown()) { throw; } throw new NLogConfigurationException("Error when including: " + newFileName, exception); } }
private string TransformKey(string key, LogEventInfo logEvent) { return(SimpleLayout.Evaluate(key, logEvent).Replace("/", "-")); }