Esempio n. 1
0
        /// <summary>
        /// Logs a message via the web service specified in App_Data\CLSSettings.config via HTTP POST.
        /// </summary>
        /// <param name="authorizeToken">The OAuth 2.0 token received from the CLS Web Service.</param>
        /// <param name="message">The Log Message to send to the Web Service.</param>
        /// <returns>Text result, empty string if failure.</returns>
        private static async Task <string> Post(string authorizeToken, WebServiceLogModel message)
        {
            // HTTP GET
            using (var client = new HttpClient())
            {
                // Initialisation
                var authorization = authorizeToken;

                // Setting Authorization
                client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", authorization);

                // Setting Base address
                client.BaseAddress = new Uri(Endpoint);

                // Setting content type
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

                // HTTP GET
                var response = await client.PostAsJsonAsync("CLS", message).ConfigureAwait(false);

                // Verification
                if (response.IsSuccessStatusCode)
                {
                    return(await response.Content.ReadAsStringAsync());
                }
            }

            return(string.Empty);
        }
    public IActionResult GetData([FromServices] Service service)
    {
        // receive model form somewhere
        WebServiceLogModel model = FetchModel();

        // enqueue model
        service.Enqueue(model);
        // TODO: return what you need
    }
Esempio n. 3
0
        // =============================================
        // Methods for logging directly to a web service
        // =============================================

        /// <summary>
        /// Logs a message via the web service specified in App_Data\CLSSettings.config.
        /// </summary>
        /// <param name="severityCode">The severity code of the log message. (Debug = D, Info = I, Warn = W, Error = E or Fatal = F).</param>
        /// <param name="exception">Optional: An exception object.</param>
        /// <param name="info">Optional: Any information to store for this record.</param>
        /// <returns>Text result, empty string if failure.</returns>
        private static async Task <string> LogToWebService(string severityCode, Exception exception = null, string info = null)
        {
            var logObj = new WebServiceLogModel
            {
                Exception          = exception?.GetExceptionMessages(),
                StackTrace         = exception?.StackTrace,
                Message            = info,
                PublishingSystemId = _publishingSystem.Id,
                Timestamp          = DateTime.Now,
                SeverityId         = StaticData.Severities.First(x => x.Code == severityCode).Id
            };

            return(await Post(OAuthResponse.access_token, logObj));
        }
        public string Post([FromBody] WebServiceLogModel logMessage)
        {
            var user = _uow.Repository <AspNetUser>().FirstOrDefault(x => x.Email == RequestContext.Principal.Identity.Name);

            if (user == null)
            {
                return($"Unable to find a user with the credentials in the current context.");
            }

            try
            {
                var exceptionHash  = HashHelper.Hash(logMessage.Exception);
                var stackTraceHash = HashHelper.Hash(logMessage.StackTrace);
                var messageHash    = HashHelper.Hash(logMessage.Message);

                var exceptionIndex =
                    _uow.Repository <LogIndexException>().FirstOrDefault(x => x.ExceptionHash == exceptionHash) ??
                    _uow.Repository <LogIndexException>().Put(new LogIndexException
                {
                    Exception     = logMessage.Exception,
                    ExceptionHash = exceptionHash
                });

                var stackTraceIndex =
                    _uow.Repository <LogIndexStackTrace>().FirstOrDefault(x => x.StackTraceHash == stackTraceHash) ??
                    _uow.Repository <LogIndexStackTrace>().Put(new LogIndexStackTrace
                {
                    StackTrace     = logMessage.StackTrace,
                    StackTraceHash = stackTraceHash
                });

                var messageIndex =
                    _uow.Repository <LogIndexMessage>().FirstOrDefault(x => x.MessageHash == messageHash) ??
                    _uow.Repository <LogIndexMessage>().Put(new LogIndexMessage
                {
                    Message     = logMessage.Message,
                    MessageHash = messageHash
                });

                _uow.Commit();

                var model = new Log
                {
                    ExceptionId        = exceptionIndex.Id,
                    StackTraceId       = stackTraceIndex.Id,
                    MessageId          = messageIndex.Id,
                    PublishingSystemId = logMessage.PublishingSystemId,
                    Timestamp          = DateTime.Now,
                    SeverityId         = logMessage.SeverityId,
                    UserId             = user.Id
                };

                _uow.Repository <Log>().Put(model);

                _uow.Commit();
            }
            catch (Exception ex)
            {
                return($"{ex.Message}");
            }

            return("Successfully logged message.");
        }
 public void Enqueue(WebServiceLogModel model)
 {
     _packets.Add(model);
 }