Ejemplo n.º 1
0
        private async void PostAsync(string typeName, string message, Exception exception, string environment, LogLevel logLevel)
        {
            var icon            = GetIcon(logLevel);
            var color           = GetColor(logLevel);
            var environmentName = string.IsNullOrEmpty(environment) ? "" : $"({environment})";

            var stackTrace = exception?.ToString();

            if (_options.SanitizeOutputFunction != null && exception != null)
            {
                stackTrace = _options.SanitizeOutputFunction(stackTrace);
            }

            var formattedStacktrace =
                exception != null
                    ? $"```\n{stackTrace.Truncate(1800)}```"
                    : string.Empty;


            var notification = ShouldNotify(logLevel) ? "<!channel>: \n" : "";

            using (var client = new HttpClient())
            {
                var payload = new
                {
                    channel     = GetChannel(logLevel),
                    username    = "******",
                    icon_emoji  = icon,
                    text        = $"{notification}*{_options.ApplicationName}* {environmentName}",
                    attachments = new[]
                    {
                        new
                        {
                            fallback  = $"Error in {_options.ApplicationName}",
                            color     = color,
                            mrkdwn_in = new[] { "fields" },
                            fields    = new[]
                            {
                                new
                                {
                                    title = "",
                                    value = $"`{typeName}`",
                                },
                                new
                                {
                                    title = $"{icon} [{logLevel}]",
                                    value = $"{message.Sanitize(_options)}",
                                },
                                new
                                {
                                    title = "",
                                    value = formattedStacktrace,
                                }
                            }
                        }
                    }
                };

                var url     = _options.WebhookUrl;
                var content = new StringContent(JsonConvert.SerializeObject(payload), Encoding.UTF8,
                                                "application/json");

                await client.PostAsync(url, content);
            }
        }
Ejemplo n.º 2
0
 public static string Sanitize(this string input, SlackLoggerOptions options)
 =>
 options.SanitizeOutputFunction != null?
 options.SanitizeOutputFunction(input) :
     input;