コード例 #1
0
        public static void SetupLogging(string username)
        {
            Username = username;
            var config = LogManager.Configuration;

            if (config == null)
            {
                config = new LoggingConfiguration();
            }

            var loggername = "asyncslack";

            if (config.FindTargetByName(loggername) != null)
            {
                config.RemoveTarget(loggername);
            }

            var slackTarget = new SlackTarget
            {
                Layout     = "${message} ${exception:format=tostring}",
                WebHookUrl = "https://hooks.slack.com/services/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",
                Channel    = "#sovnd-client-logs",
                Username   = username,
                Compact    = true
            };

            AsyncTargetWrapper asyncSlack = new AsyncTargetWrapper(slackTarget);

            config.AddTarget(loggername, asyncSlack);
            var slackTargetRules = new LoggingRule("*", LogLevel.Error, asyncSlack);

            config.LoggingRules.Add(slackTargetRules);


            loggername = "asyncmem";
            if (memoryTarget == null)
            {
                memoryTarget = new MemoryTarget {
                    Layout = "${message} ${exception:format=tostring}"
                };
                config.AddTarget(loggername, memoryTarget);

                var memTargetRules = new LoggingRule("*", LogLevel.Trace, memoryTarget);
                config.LoggingRules.Add(memTargetRules);

                if (config.FindTargetByName(loggername) != null)
                {
                    config.RemoveTarget(loggername);
                }
            }

            LogManager.Configuration = config;
            BugSenseHandler.Instance.UserIdentifier = username;

            var ver = Assembly.GetExecutingAssembly().GetName().Version.ToString();

            LogTo.Error("SOVND Ver {0} running as {1}", ver, username);
        }
コード例 #2
0
        public void InvalidCodeConfig_Tests()
        {
            {
                var configuration = new LoggingConfiguration();
                var target        = new SlackTarget
                {
                    SerializerType = typeof(JilHttpContentConverter),
                    Layout         = "${longdate}|${level}|${message}"
                };

                configuration.AddTarget("slack", target);
                configuration.LoggingRules.Add(new LoggingRule("*", LogLevel.Debug, target));

                var factory = new LogFactory(configuration);
                var logger  = factory.GetCurrentClassLogger();

                // It dosen't send log.
                logger.Info("nlog.invalidxml.test");
            }

            {
                var configuration = new LoggingConfiguration();
                var target        = new SlackTarget
                {
                    WebHookUrl = WebHookUrl,
                    Layout     = "${longdate}|${level}|${message}"
                };

                configuration.AddTarget("slack", target);
                configuration.LoggingRules.Add(new LoggingRule("*", LogLevel.Debug, target));

                var factory = new LogFactory(configuration);
                var logger  = factory.GetCurrentClassLogger();

                // It dosen't send log.
                logger.Info("nlog.invalidxml.test");
            }

            {
                var configuration = new LoggingConfiguration();
                var target        = new SlackTarget
                {
                    WebHookUrl     = WebHookUrl,
                    SerializerType = typeof(DummySerializer),
                    Layout         = "${longdate}|${level}|${message}"
                };

                configuration.AddTarget("slack", target);
                configuration.LoggingRules.Add(new LoggingRule("*", LogLevel.Debug, target));

                var factory = new LogFactory(configuration);
                var logger  = factory.GetCurrentClassLogger();

                // It dosen't send log.
                logger.Info("nlog.invalidxml.test");
            }
        }
コード例 #3
0
        private static void BootstrapNLog()
        {
            GlobalDiagnosticsContext.Set("ServiceInstanceId", Guid.NewGuid().ToString("N"));

            var config             = new LoggingConfiguration();
            var colorConsoleTarget = new ColoredConsoleTarget()
            {
                Name = "colorConsole"
            };

            config.AddTarget("colorConsole", colorConsoleTarget);
            config.LoggingRules.Add(new LoggingRule("*", LogLevel.Trace, colorConsoleTarget));

            var databaseTarget = new DatabaseTarget()
            {
                ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString,
                CommandText      = "INSERT INTO EventLog (Logger, [TimeStamp], Level, ServiceInstanceId, JobId, Message, Exception, StackTrace) " +
                                   "VALUES (@logger, @timeStamp, @level, @serviceinstanceid, @jobid, " +
                                   "CASE WHEN LEN(@message) > 4000 THEN LEFT(@message, 3988) + '[truncated]' ELSE @message END," +
                                   "CASE WHEN LEN(@exception) > 4000 THEN LEFT(@exception, 3988) + '[truncated]' ELSE @exception END," +
                                   "CASE WHEN LEN(@stacktrace) > 4000 THEN LEFT(@stacktrace, 3988) + '[truncated]' ELSE @stacktrace END" +
                                   ")",
                Parameters =
                {
                    new DatabaseParameterInfo("@logger",            new SimpleLayout("${logger}")),
                    new DatabaseParameterInfo("@timestamp",         new SimpleLayout("${date}")),
                    new DatabaseParameterInfo("@level",             new SimpleLayout("${level}")),
                    new DatabaseParameterInfo("@serviceinstanceid", new SimpleLayout("${gdc:item=ServiceInstanceId}")),
                    new DatabaseParameterInfo("@jobid",             new SimpleLayout("${mdc:item=JobId}")),
                    new DatabaseParameterInfo("@message",           new SimpleLayout("${message}")),
                    new DatabaseParameterInfo("@exception",         new SimpleLayout("${exception}")),
                    new DatabaseParameterInfo("@stacktrace",        new SimpleLayout("${exception:stacktrace}")),
                }
            };

            config.AddTarget("database", databaseTarget);
            config.LoggingRules.Add(new LoggingRule("*", LogLevel.Info, databaseTarget));

            var slackTarget = new SlackTarget()
            {
                ChannelUrl = "https://hooks.slack.com/services/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", MessagePrefix = Environment.MachineName
            };

            config.AddTarget("slack", slackTarget);
            config.LoggingRules.Add(new LoggingRule("*", LogLevel.Warn, slackTarget));

            LogManager.Configuration = config;
        }
コード例 #4
0
        public static LoggingConfiguration UseSlack(this LoggingConfiguration config, LogConfig configuration)
        {
            if (!string.IsNullOrEmpty(configuration.Slack?.TelemetryKey))
            {
                var slackTarget = new SlackTarget
                {
                    Layout     = "${message}",
                    WebHookUrl = "https://hooks.slack.com/services/" + configuration.Slack.TelemetryKey,
                    Channel    = configuration.Slack.Channel,
                    Username   = configuration.ApplicationName,
                    Compact    = true
                };

                config.LoggingRules.Add(new LoggingRule("*", LogLevel.FromString(configuration.Slack.LogLevel ?? "Warn"), slackTarget));
            }

            return(config);
        }
        public Logger ConfigureLogger()
        {
            var config = new LoggingConfiguration();

            var consoleTarget = new ColoredConsoleTarget()
            {
                Name   = "console",
                Layout = "${date}|${level:uppercase=true}|${message} ${exception}|${logger}|${all-event-properties}"
            };

            config.AddTarget(consoleTarget);
            config.AddRuleForAllLevels(consoleTarget);

            var fileTarget = new FileTarget()
            {
                Name     = "localerrorfile",
                FileName = "${currentdir}/errors.log",
                Layout   = "${date}|${level:uppercase=true}|${message} ${exception}|${logger}|${all-event-properties}"
            };

            config.AddTarget(fileTarget);
            config.AddRuleForOneLevel(LogLevel.Error, fileTarget);

            if (!string.IsNullOrWhiteSpace(_configuration.GetValue <string>("SlackWebhookUrl")))
            {
                var slackTarget = new SlackTarget()
                {
                    Name       = "slack",
                    WebHookUrl = _configuration.GetValue <string>("SlackWebhookUrl"),
                    Layout     = "[${level:uppercase=true}]\n${message}\n${exception}"
                                 // Add Fields and such
                };
                slackTarget.Compact = true;
                config.AddTarget(slackTarget);
                config.AddRuleForOneLevel(LogLevel.Error, slackTarget);
            }

            InternalLogger.LogFile     = "internal-nlog.txt";
            InternalLogger.LogLevel    = LogLevel.Info;
            LogManager.ThrowExceptions = true;
            LogManager.Configuration   = config;

            return(LogManager.GetCurrentClassLogger());
        }
コード例 #6
0
        private static void SetNLogSlackTarget()
        {
            string slackWebhookUrl = Environment.GetEnvironmentVariable("KBS_SLACK_WEBHOOK");

            if (String.IsNullOrEmpty(slackWebhookUrl))
            {
                throw new ApplicationException("Slack webhook URL must be provided via the KBS_SLACK_WEBHOOK environment variable.");
            }

            var configuration = LogManager.Configuration;
            var targets       = configuration.AllTargets;

            // N.B. This returns null so have to find all targets and then cast!
            //SlackTarget slackTarget = configuration.FindTargetByName<SlackTarget>("slackTarget");
            SlackTarget slackTarget = (SlackTarget)targets.First(t => t.GetType() == typeof(SlackTarget));

            slackTarget.WebHookUrl   = slackWebhookUrl;
            LogManager.Configuration = configuration;
        }
コード例 #7
0
        public void ValidCodeConfig_Tests()
        {
            var configuration = new LoggingConfiguration();
            var target        = new SlackTarget
            {
                WebHookUrl     = WebHookUrl,
                SerializerType = typeof(JilHttpContentConverter),
                Layout         = "${longdate}|${level}|${message}"
            };

            configuration.AddTarget("slack", target);
            configuration.LoggingRules.Add(new LoggingRule("*", LogLevel.Debug, target));

            var factory = new LogFactory(configuration);
            var logger  = factory.GetCurrentClassLogger();

            logger.Trace("nlog.validcode.trace");
            logger.Debug("nlog.validcode.debug");
            logger.Info("nlog.validcode.info");
            logger.Warn("nlog.validcode.warn");
            logger.Error("nlog.validcode.error");
            logger.Fatal("nlog.validcode.fatal");
        }
コード例 #8
0
ファイル: Message.cs プロジェクト: siketyan/HookLab
 public Builder(UserDictionaryManager userDictionaryManager, SlackTarget target)
 {
     _userDictionaryManager = userDictionaryManager;
     _target = target;
 }