예제 #1
0
        public IHttpActionResult Log([FromBody] ServiceLogModel logEntry)
        {
            string ipAdress = GetIpAddress();

            switch (logEntry.LogLevel)
            {
            case LogLevelEnum.Error:
                BlueprintEventSource.Log.Error(
                    ipAdress,
                    logEntry.Source,
                    logEntry.Message,
                    logEntry.OccurredAt,
                    logEntry.MethodName,
                    logEntry.FilePath,
                    logEntry.LineNumber,
                    logEntry.StackTrace);
                break;

            case LogLevelEnum.Warning:
                BlueprintEventSource.Log.Warning(
                    ipAdress,
                    logEntry.Source,
                    logEntry.Message,
                    logEntry.OccurredAt,
                    logEntry.MethodName,
                    logEntry.FilePath,
                    logEntry.LineNumber);
                break;

            case LogLevelEnum.Informational:
                BlueprintEventSource.Log.Informational(
                    ipAdress,
                    logEntry.Source,
                    logEntry.Message,
                    logEntry.OccurredAt,
                    logEntry.MethodName,
                    logEntry.FilePath,
                    logEntry.LineNumber);
                break;

            case LogLevelEnum.Verbose:
                BlueprintEventSource.Log.Verbose(
                    ipAdress,
                    logEntry.Source,
                    logEntry.Message,
                    logEntry.OccurredAt,
                    logEntry.MethodName,
                    logEntry.FilePath,
                    logEntry.LineNumber);
                break;

            default:
                break;
            }

            return(Ok());
        }
예제 #2
0
        public void Log_Informational_ReturnsOk()
        {
            // Arrange
            var controller = new LogController();
            var logEntry   = new ServiceLogModel
            {
                LogLevel   = LogLevelEnum.Informational,
                Source     = "Controller source",
                Message    = "Hello",
                OccurredAt = DateTime.Now
            };

            // Act
            var result = controller.Log(logEntry);

            // Assert
            Assert.IsInstanceOfType(result, typeof(OkResult));
        }
예제 #3
0
        /// <summary>
        /// LogInformation
        /// </summary>
        /// <param name="source">source</param>
        /// <param name="message">message</param>
        /// <param name="methodName">Do not pass a value - compiler will fill this in</param>
        /// <param name="filePath">Do not pass a value - compiler will fill this in</param>
        /// <param name="lineNumber">Do not pass a value - compiler will fill this in</param>
        /// <example>
        /// var servicelog = new ServiceLogRepository();
        /// await servicelog.LogInformation("FileStore API", "Hello World");
        /// </example>
        public async Task LogInformation(
            string source,
            string message,
            [CallerMemberName] string methodName = "",
            [CallerFilePath] string filePath     = "",
            [CallerLineNumber] int lineNumber    = 0)
        {
            try
            {
                var uri = _configControlUri;
                if (string.IsNullOrWhiteSpace(uri))
                {
                    throw new ApplicationException("Application setting not set: ConfigControl");
                }
                var http = _httpClientProvider.Create(new Uri(uri));

                // create the log entry
                var logEntry = new ServiceLogModel
                {
                    LogLevel   = LogLevelEnum.Informational,
                    Source     = source,
                    Message    = message,
                    OccurredAt = DateTime.Now,
                    MethodName = methodName,
                    FilePath   = filePath,
                    LineNumber = lineNumber,
                    StackTrace = ""
                };

                // Convert Object to JSON
                var requestMessage = JsonConvert.SerializeObject(logEntry);
                var content        = new StringContent(requestMessage, Encoding.UTF8, "application/json");

                HttpResponseMessage response = await http.PostAsync("log", content);

                response.EnsureSuccessStatusCode();
            }
            catch (Exception ex)
            {
                _localLog.LogErrorFormat("Problem with ConfigControl Log service: {0}", ex.Message);
            }
        }