Beispiel #1
0
        private void InitLog()
        {
            // Set-up logging with NLog. Per post below, need to store XML in source code or configure in code.
            // https://github.com/NLog/NLog/wiki/Tutorial
            // https://stackoverflow.com/questions/40602775/nlog-does-not-write-to-log-file-when-called-from-outlook-add-in
            var config = new NLog.Config.LoggingConfiguration();

            // Targets where to log to: File and Console
            String layout  = "${longdate}|${level:uppercase=true}|${logger}|${message} ${exception:format=ToString}";
            var    logfile = new NLog.Targets.FileTarget("logfile")
            {
                FileName         = System.IO.Path.Combine("${specialfolder:folder=ApplicationData}", "Outlook QuickFile", "log.${shortdate}.txt"),
                Layout           = layout,
                MaxArchiveFiles  = 4,
                ArchiveAboveSize = 10240,
            };
            var logconsole = new NLog.Targets.TraceTarget("logconsole")// ConsoleTarget doesn't work for VSTO plugins.
            {
                Layout = layout,
            };

            // Rules for mapping loggers to targets
            config.AddRule(NLog.LogLevel.Debug, NLog.LogLevel.Fatal, logconsole);
            config.AddRule(NLog.LogLevel.Info, NLog.LogLevel.Fatal, logfile);

            // Apply config
            NLog.LogManager.Configuration = config;
        }
Beispiel #2
0
        public static void Main(string[] args)
        {
            Program.Journal.journal = new Dictionary <int, List <string> > {
            };

            var config = new NLog.Config.LoggingConfiguration();

            var logfile = new NLog.Targets.FileTarget("logfile")
            {
                FileName     = "log.txt",
                ArchiveEvery = NLog.Targets.FileArchivePeriod.Day,
                Layout       = "${longdate} | ${level:uppercase=true} | ${message}",
            };

            var logtrace = new NLog.Targets.TraceTarget("logtrace")
            {
                Name   = "trace",
                Layout = "${logger} ${message}"
            };

            var logconsole = new NLog.Targets.ConsoleTarget("logconsole");

            config.AddRule(NLog.LogLevel.Info, NLog.LogLevel.Fatal, logconsole);
            config.AddRule(NLog.LogLevel.Debug, NLog.LogLevel.Fatal, logfile);

            config.AddRule(NLog.LogLevel.Trace, NLog.LogLevel.Fatal, logtrace);

            NLog.LogManager.Configuration = config;


            CreateWebHostBuilder(args).Build().Run();
        }
Beispiel #3
0
        private static void StartLogger()
        {
            //logging
            var config        = new NLog.Config.LoggingConfiguration();
            var traceTarget   = new NLog.Targets.TraceTarget();
            var consoleTarget = new NLog.Targets.ConsoleTarget();

            config.AddRule(LogLevel.Trace, LogLevel.Fatal, traceTarget);
            config.AddRule(LogLevel.Trace, LogLevel.Fatal, consoleTarget);
            LogManager.Configuration = config;
            LogManager.GetCurrentClassLogger().Info("Application started.");
        }
        private void OnInitialize()
        {
            // Add azure trace listener:
            var traceListener = new DiagnosticMonitorTraceListener();

            Trace.Listeners.Add(traceListener);

            // Configure diagnostics monitor:
            var dmc = DiagnosticMonitor.GetDefaultInitialConfiguration();

            // Log level is defined by nlog:
            dmc.Logs.ScheduledTransferLogLevelFilter = LogLevel.Verbose;

            // Configure log transfer period:
            var transferPeriod = TimeSpan.FromMinutes(ConfigurationProvider.GetSetting(ConfigurationSettingsKeys.DiagnosticsTransferPeriodInMinutes, 0.3));

            dmc.Logs.ScheduledTransferPeriod            = transferPeriod;
            dmc.WindowsEventLog.ScheduledTransferPeriod = transferPeriod;

            // Add any event logs we want to see:
            foreach (var eventLogName in ConfigurationProvider.GetSetting(ConfigurationSettingsKeys.DiagnosticsEventLogsToTransfer, "Application!*;System!*").Split(';'))
            {
                dmc.WindowsEventLog.DataSources.Add(eventLogName);
            }

            // Initialize the monitor:
            DiagnosticMonitor.Start(ConfigurationSettingsKeys.DiagnosticsConnectionString, dmc);

            // Create nlog configuration:
            var loggingConfiguration = new LoggingConfiguration();
            var logLayout            = ConfigurationProvider.GetSetting(ConfigurationSettingsKeys.DiagnosticsLogLayout, "${date} ${gdc:item=InstanceId} ${message} ${exception}");

            var traceTarget = new NLog.Targets.TraceTarget()
            {
                Layout = logLayout
            };

            loggingConfiguration.AddTarget("trace", traceTarget);

            var logLevelName = ConfigurationProvider.GetSetting(ConfigurationSettingsKeys.DiagnosticsLogLevel, "Debug");
            var logLevel     = (NLog.LogLevel) typeof(NLog.LogLevel).GetField(logLevelName, BindingFlags.Public | BindingFlags.Static).GetValue(null);

            loggingConfiguration.LoggingRules.Add(new LoggingRule("*", logLevel, traceTarget));

            GlobalDiagnosticsContext.Set("InstanceId", RoleEnvironment.CurrentRoleInstance.Id);
            LogManager.Configuration = loggingConfiguration;
        }
        private void OnInitialize()
        {
            // Add azure trace listener:
            var traceListener = new DiagnosticMonitorTraceListener();
            Trace.Listeners.Add(traceListener);

            // Configure diagnostics monitor:
            var dmc = DiagnosticMonitor.GetDefaultInitialConfiguration();

            // Log level is defined by nlog:
            dmc.Logs.ScheduledTransferLogLevelFilter    = LogLevel.Verbose;

            // Configure log transfer period:
            var transferPeriod                          = TimeSpan.FromMinutes(ConfigurationProvider.GetSetting(ConfigurationSettingsKeys.DiagnosticsTransferPeriodInMinutes,0.3));

            dmc.Logs.ScheduledTransferPeriod            = transferPeriod;
            dmc.WindowsEventLog.ScheduledTransferPeriod = transferPeriod;

            // Add any event logs we want to see:
            foreach (var eventLogName in ConfigurationProvider.GetSetting(ConfigurationSettingsKeys.DiagnosticsEventLogsToTransfer, "Application!*;System!*").Split(';'))
            {
                dmc.WindowsEventLog.DataSources.Add(eventLogName);
            }

            // Initialize the monitor:
            DiagnosticMonitor.Start(ConfigurationSettingsKeys.DiagnosticsConnectionString, dmc);

            // Create nlog configuration:
            var loggingConfiguration    = new LoggingConfiguration();
            var logLayout = ConfigurationProvider.GetSetting(ConfigurationSettingsKeys.DiagnosticsLogLayout, "${date} ${gdc:item=InstanceId} ${message} ${exception}");

            var traceTarget = new NLog.Targets.TraceTarget() {Layout = logLayout};
            loggingConfiguration.AddTarget("trace", traceTarget);

            var logLevelName    = ConfigurationProvider.GetSetting(ConfigurationSettingsKeys.DiagnosticsLogLevel, "Debug");
            var logLevelField   = typeof (NLog.LogLevel).GetField(logLevelName, BindingFlags.Public | BindingFlags.Static);
            var logLevel        = logLevelField != null ? (NLog.LogLevel) logLevelField.GetValue(null) : NLog.LogLevel.Debug;

            loggingConfiguration.LoggingRules.Add(new LoggingRule("*", logLevel, traceTarget));

            GlobalDiagnosticsContext.Set("InstanceId",RoleEnvironment.CurrentRoleInstance.Id);
            LogManager.Configuration = loggingConfiguration;
        }
Beispiel #6
0
        protected override void Configure(TestConfiguration config, Random random)
        {
            base.Configure(config, random);

            var c = new NLog.Config.LoggingConfiguration();
            var traceTarget = new NLog.Targets.TraceTarget();
            traceTarget.Layout = "${message}";
            c.AddTarget("trace", traceTarget);

            var rule1 = new NLog.Config.LoggingRule("*", NLog.LogLevel.Error, traceTarget);
            c.LoggingRules.Add(rule1);

            NLog.LogManager.Configuration = c;

            Logger =  NLog.LogManager.GetCurrentClassLogger();
        }