Esempio n. 1
0
    public static LogFactory CreateLogFactory(TargetWithLayout inMemoryLogs)
    {
        var loggingConfiguration = new LoggingConfiguration();

        ConfigureAndAddLoggingTarget(
            inMemoryLogs,
            loggingConfiguration);
        return(new LogFactory(loggingConfiguration));
    }
Esempio n. 2
0
    public static LogFactory CreateLogFactory(TargetWithLayout target)
    {
        var loggingConfiguration = new LoggingConfiguration();

        ConfigForLogger.ConfigureAndAddLoggingTarget(
            target,
            loggingConfiguration);
        return(new LogFactory(loggingConfiguration));
    }
Esempio n. 3
0
        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);
        }
Esempio n. 4
0
        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);
        }
Esempio n. 5
0
 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);
                }
            }
        }
Esempio n. 7
0
        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);
        }
Esempio n. 8
0
        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;
        }
Esempio n. 9
0
 public static void ShouldHaveLogged(this TargetWithLayout target, string message)
 {
     MemoryTarget(target).Logs.ShouldContain(message);
 }
Esempio n. 10
0
        public static void ShouldHaveLogged(this TargetWithLayout target, Func <string, bool> predicate)
        {
            var expression = FuncToExpression(predicate);

            MemoryTarget(target).Logs.ShouldContain(expression);
        }
Esempio n. 11
0
        /// <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);
                }
            }
        }