コード例 #1
0
        // ReSharper disable once InconsistentNaming
        public void LogMessageFromWebClient_MessageIsValidJsonString()
        {
            const string testLogMessage = "Тестовое сообщение \n строка 2 \n \"текст в кавычках\"";

            var webClientLoggingService = new WebClientLoggingService(GetMockedLogger((message, parameters) =>
            {
                var receivedMessage = JsonConvert.DeserializeObject <string>($"\"{message}\"");
                receivedMessage.ShouldBe(testLogMessage);
            }));

            var messageItem = new WebClientLogInfo
            {
                Message          = testLogMessage,
                AdditionalParams = new Dictionary <string, string>(),
                Level            = "",
                Stacktrace       = "",
                ClientVersion    = "0.1.0"
            };
            var logs = new List <WebClientLogInfo> {
                messageItem
            };

            webClientLoggingService.Log(new WebClientLogs {
                Logs = logs
            });
        }
コード例 #2
0
        private void Log(WebClientLogInfo logInfo)
        {
            IDictionary <string, string> commonParams = CreateCommonParams(logInfo);

            var message = NormalizeLogString(logInfo.Message);

            _logger.Information(message, properties: commonParams);
        }
コード例 #3
0
        /// <summary>
        /// Создать дополнительные параметры для отправки вместе с сообщением
        /// Все дополнительные параметры от клиента имеют префикс "webclient_"
        /// </summary>
        /// <param name="logInfo"></param>
        /// <returns></returns>
        private IDictionary <string, string> CreateCommonParams(WebClientLogInfo logInfo)
        {
            var parameters = new Dictionary <string, string>
            {
                [WebClientPrefix]            = true.ToString(),
                [CreateKey("level")]         = logInfo.Level,
                [CreateKey("clientVersion")] = logInfo.ClientVersion,
                [CreateKey("level")]         = logInfo.Level,
                [CreateKey("stacktrace")]    = NormalizeLogString(logInfo.Stacktrace),
                [CreateKey("gisLogs")]       = logInfo.IsGisLogs.ToString(),
            };

            foreach ((string key, string value) in logInfo.AdditionalParams)
            {
                parameters[key] = value;
            }

            return(parameters);
        }