public static LogFactory CreateLogFactory(TargetWithLayout inMemoryLogs) { var loggingConfiguration = new LoggingConfiguration(); ConfigureAndAddLoggingTarget( inMemoryLogs, loggingConfiguration); return(new LogFactory(loggingConfiguration)); }
public static LogFactory CreateLogFactory(TargetWithLayout target) { var loggingConfiguration = new LoggingConfiguration(); ConfigForLogger.ConfigureAndAddLoggingTarget( target, loggingConfiguration); return(new LogFactory(loggingConfiguration)); }
private static MemoryTarget MemoryTarget(TargetWithLayout target) { var memoryTarget = target as MemoryTarget; if (memoryTarget == null) { throw new ArgumentNullException("target", "target must derive from NLog.Targets.MemoryTarget"); } return(memoryTarget); }
internal static LogFactory CreateTestLogFactory(TargetWithLayout target) { var config = new LoggingConfiguration(); target.Layout = "${level:lowercase=true}: [${logger}] ${message}"; config.AddTarget("test", target); var rule = new LoggingRule("*", LogLevel.Trace, target); config.LoggingRules.Add(rule); return new LogFactory(config); }
public static void ConfigureAndAddLoggingTarget(TargetWithLayout target, LoggingConfiguration loggingConfiguration) { target.Layout = Layout.FromString( //bug FromMethod allows even more customization "${longdate}" + "|${event-properties:item=EventId_Id:whenEmpty=0}" + "|${uppercase:${level}}" + "|${logger}" + "|${message} ${exception:format=tostring}" + "|requestId=${mdlc:item=requestId}" + "|operationName=${mdlc:item=operationName}" + "|customerId=${mdlc:item=customerId}"); loggingConfiguration.AddRuleForAllLevels(target); }
/// <summary> /// Assigns the target to the Roadkill rule and sets its layout to [date] [level] [message] /// </summary> private static void AddNLogTarget(TargetWithLayout target, string name) { target.Layout = DEFAULT_LAYOUT; target.Name = name; if (!LogManager.Configuration.AllTargets.Contains(target)) { LogManager.Configuration.AddTarget(name, target); LoggingRule rule = new LoggingRule(LOGGER_NAME, target); if (!LogManager.Configuration.LoggingRules.Contains(rule)) { LogManager.Configuration.LoggingRules.Add(rule); } } }
public static void SetupLogging() { var logFileEnvVarValue = Environment.GetEnvironmentVariable(LogFileEnvVarName); if (logFileEnvVarValue == null) { return; } var config = new LoggingConfiguration(); const string layout = "${date:format=yyyy-MM-dd HH\\:mm\\:ss.fff}" + " | ${level:uppercase=true:padding=-5}" + // negative values cause right padding " | ${threadname:padding=-30:whenEmpty=${threadid:padding=-30}}" + "${when:when=length('${logger}') > 0:inner= | ${logger}}" + " | ${message}" + "${onexception:${newline}${exception:format=ToString}"; var logTargets = new TargetWithLayout[] { LogMemoryTarget, new FileTarget { FileName = logFileEnvVarValue, DeleteOldFileOnStartup = true }, new ConsoleTarget() }; foreach (var logTarget in logTargets) { logTarget.Layout = layout; } // ReSharper disable once CoVariantArrayConversion config.AddRule(LogLevel.Trace, LogLevel.Fatal, new SplitGroupTarget(logTargets)); InternalLogger.LogToConsole = true; InternalLogger.LogFile = logFileEnvVarValue; InternalLogger.LogLevel = LogLevel.Info; InternalLogger.LogWriter = new StringWriter(); // Apply NLog config LogManager.Configuration = config; // Set up Elastic APM .NET Agent to use NLog for its logging AgentDependencies.Logger = new ApmLoggerToNLog(); Logger.Debug(nameof(SetupLogging) + " completed. Path to log file: {SampleAppLogFilePath}", logFileEnvVarValue); }
void Awake() { LoggingConfiguration config = new LoggingConfiguration(); List <TargetWithLayout> targets = new List <TargetWithLayout>(); for (int i = 0; i < logTargets.Length; i++) { Type t = Type.GetType(logTargets[i]); if (t == null) { Debug.LogWarning("Could not get logger target of type " + logTargets[i]); continue; } TargetWithLayout target = System.Activator.CreateInstance(t) as TargetWithLayout; if (target == null) { Debug.LogError("Couldn't instantiate logger: " + logTargets[i]); continue; } target.Layout = new SimpleLayout("[${logger}] ${message}"); targets.Add(target); } SplitGroupTarget splitTarget = new SplitGroupTarget(targets.ToArray()); config.AddTarget("Split Target", splitTarget); LogManager.Configuration = config; for (int i = 0; i < logSources.Length; i++) { string logName = logSources[i].name; LogLevel logLevel = ToLogLevel(logSources[i].logLevel); AddRule(config, logName, logLevel, splitTarget); } LogManager.Configuration = config; }
public static void ShouldHaveLogged(this TargetWithLayout target, string message) { MemoryTarget(target).Logs.ShouldContain(message); }
public static void ShouldHaveLogged(this TargetWithLayout target, Func <string, bool> predicate) { var expression = FuncToExpression(predicate); MemoryTarget(target).Logs.ShouldContain(expression); }