Esempio n. 1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="_Messages"></param>
        /// <param name="_LogGroupName"></param>
        /// <param name="_LogStreamName"></param>
        /// <param name="_bAsync"></param>
        /// <param name="_ErrorMessageAction"></param>
        /// <returns></returns>
        public bool WriteLogs(List <BLoggingParametersStruct> _Messages, string _LogGroupName, string _LogStreamName, bool _bAsync = true, Action <string> _ErrorMessageAction = null)
        {
            if (_Messages == null || _Messages.Count == 0)
            {
                return(false);
            }

            if (_bAsync)
            {
                BTaskWrapper.Run(() =>
                {
                    WriteLogs(_Messages, _LogGroupName, _LogStreamName, false, _ErrorMessageAction);
                });
                return(true);
            }
            else
            {
                _LogGroupName  = BUtility.EncodeStringForTagging(_LogGroupName);
                _LogStreamName = BUtility.EncodeStringForTagging(_LogStreamName);

                if (!BUtility.CalculateStringMD5(DateTime.Now.Subtract(DateTime.MinValue.AddYears(1969)).TotalMilliseconds.ToString(), out string Timestamp, _ErrorMessageAction))
                {
                    _ErrorMessageAction?.Invoke("BLoggingServiceAzure->WriteLogs: Timestamp generation has failed.");
                    return(false);
                }

                string StreamIDBase = _LogGroupName + "-" + _LogStreamName + "-" + Timestamp;

                try
                {
                    using (AzureLogger.BeginScope(StreamIDBase))
                    {
                        foreach (var Message in _Messages)
                        {
                            var level   = LogLevel.Information;
                            var message = "Info-> " + Message.Message;

                            switch (Message.LogType)
                            {
                            case EBLoggingServiceLogType.Debug:
                                level   = LogLevel.Debug;
                                message = "Debug-> " + Message.Message;
                                break;

                            case EBLoggingServiceLogType.Warning:
                                level   = LogLevel.Warning;
                                message = "Warning-> " + Message.Message;
                                break;

                            case EBLoggingServiceLogType.Error:
                                level   = LogLevel.Error;
                                message = "Error-> " + Message.Message;
                                break;

                            case EBLoggingServiceLogType.Critical:
                                level   = LogLevel.Critical;
                                message = "Critical-> " + Message.Message;
                                break;

                            default:
                                level   = LogLevel.Information;
                                message = "Info-> " + Message.Message;
                                break;
                            }

                            AzureLogger.Log(level, message);
                        }
                    }

                    // Explicitly call Flush() followed by sleep is required in Console Apps.
                    // This is to ensure that even if application terminates, telemetry is sent to the back-end.
                    AzureTelemetryChannel?.Flush();
                    Thread.Sleep(1000);

                    return(true);
                }
                catch (Exception e)
                {
                    _ErrorMessageAction?.Invoke("BLoggingServiceAzure->WriteLogs: " + e.Message + ", Trace: " + e.StackTrace);
                }
            }

            return(false);
        }
Esempio n. 2
0
        /// <summary>
        ///
        /// <para>WriteLogs:</para>
        ///
        /// <para>Writes logs to the logging service</para>
        ///
        /// <para>Check <seealso cref="IBLoggingServiceInterface.WriteLogs"/> for detailed documentation</para>
        ///
        /// </summary>
        public bool WriteLogs(
            List <BLoggingParametersStruct> _Messages,
            string _LogGroupName,
            string _LogStreamName,
            bool _bAsync = true,
            Action <string> _ErrorMessageAction = null)
        {
            if (_Messages == null || _Messages.Count == 0)
            {
                return(false);
            }

            if (_bAsync)
            {
                BTaskWrapper.Run(() =>
                {
                    WriteLogs(_Messages, _LogGroupName, _LogStreamName, false, _ErrorMessageAction);
                });
                return(true);
            }
            else
            {
                _LogGroupName  = BUtility.EncodeStringForTagging(_LogGroupName);
                _LogStreamName = BUtility.EncodeStringForTagging(_LogStreamName);

                string SequenceToken = null;

                bool bLogStreamAndGroupExists = false;
                try
                {
                    var DescribeStreamRequest = new DescribeLogStreamsRequest(_LogGroupName);
                    using (var CreatedDescribeTask = CloudWatchLogsClient.DescribeLogStreamsAsync(DescribeStreamRequest))
                    {
                        CreatedDescribeTask.Wait();
                        if (CreatedDescribeTask.Result != null && CreatedDescribeTask.Result.LogStreams != null && CreatedDescribeTask.Result.LogStreams.Count > 0)
                        {
                            foreach (var Current in CreatedDescribeTask.Result.LogStreams)
                            {
                                if (Current != null && Current.LogStreamName == _LogStreamName)
                                {
                                    SequenceToken            = Current.UploadSequenceToken;
                                    bLogStreamAndGroupExists = true;
                                    break;
                                }
                            }
                        }
                    }
                }
                catch (Exception)
                {
                    bLogStreamAndGroupExists = false;
                }

                if (!bLogStreamAndGroupExists)
                {
                    try
                    {
                        var CreateGroupRequest = new CreateLogGroupRequest(_LogGroupName);
                        using (var CreatedGroupTask = CloudWatchLogsClient.CreateLogGroupAsync(CreateGroupRequest))
                        {
                            CreatedGroupTask.Wait();
                        }
                    }
                    catch (Exception e)
                    {
                        if (!(e is ResourceAlreadyExistsException))
                        {
                            _ErrorMessageAction?.Invoke("BLoggingServiceAWS->WriteLogs: " + e.Message + ", Trace: " + e.StackTrace);
                            return(false);
                        }
                    }

                    try
                    {
                        var CreateStreamRequest = new CreateLogStreamRequest(_LogGroupName, _LogStreamName);
                        using (var CreatedStreamTask = CloudWatchLogsClient.CreateLogStreamAsync(CreateStreamRequest))
                        {
                            CreatedStreamTask.Wait();
                        }
                    }
                    catch (Exception e)
                    {
                        if (!(e is ResourceAlreadyExistsException))
                        {
                            _ErrorMessageAction?.Invoke("BLoggingServiceAWS->WriteLogs: " + e.Message + ", Trace: " + e.StackTrace);
                            return(false);
                        }
                    }
                }

                var LogEvents = new List <InputLogEvent>();
                foreach (var Message in _Messages)
                {
                    var LogEvent = new InputLogEvent()
                    {
                        Message   = Message.Message,
                        Timestamp = DateTime.UtcNow
                    };

                    switch (Message.LogType)
                    {
                    case EBLoggingServiceLogType.Debug:
                        LogEvent.Message = "Debug-> " + LogEvent.Message;
                        break;

                    case EBLoggingServiceLogType.Info:
                        LogEvent.Message = "Info-> " + LogEvent.Message;
                        break;

                    case EBLoggingServiceLogType.Warning:
                        LogEvent.Message = "Warning-> " + LogEvent.Message;
                        break;

                    case EBLoggingServiceLogType.Error:
                        LogEvent.Message = "Error-> " + LogEvent.Message;
                        break;

                    case EBLoggingServiceLogType.Critical:
                        LogEvent.Message = "Critical-> " + LogEvent.Message;
                        break;
                    }

                    LogEvents.Add(LogEvent);
                }

                try
                {
                    var PutRequest = new PutLogEventsRequest(_LogGroupName, _LogStreamName, LogEvents)
                    {
                        SequenceToken = SequenceToken
                    };
                    using (var CreatedPutTask = CloudWatchLogsClient.PutLogEventsAsync(PutRequest))
                    {
                        CreatedPutTask.Wait();
                    }
                }
                catch (Exception e)
                {
                    _ErrorMessageAction?.Invoke("BLoggingServiceAWS->WriteLogs: " + e.Message + ", Trace: " + e.StackTrace);
                    return(false);
                }
                return(true);
            }
        }
Esempio n. 3
0
        /// <summary>
        ///
        /// <para>WriteLogs:</para>
        ///
        /// <para>Writes logs to the logging service</para>
        ///
        /// <para>Check <seealso cref="IBLoggingServiceInterface.WriteLogs"/> for detailed documentation</para>
        ///
        /// </summary>
        public bool WriteLogs(
            List <BLoggingParametersStruct> _Messages,
            string _LogGroupName,
            string _LogStreamName,
            bool _bAsync = true,
            Action <string> _ErrorMessageAction = null)
        {
            if (_Messages == null || _Messages.Count == 0)
            {
                return(false);
            }

            if (_bAsync)
            {
                BTaskWrapper.Run(() =>
                {
                    WriteLogs(_Messages, _LogGroupName, _LogStreamName, false, _ErrorMessageAction);
                });
                return(true);
            }
            else
            {
                if (!BUtility.CalculateStringMD5(DateTime.Now.Subtract(DateTime.MinValue.AddYears(1969)).TotalMilliseconds.ToString(), out string Timestamp, _ErrorMessageAction))
                {
                    _ErrorMessageAction?.Invoke("BLoggingServiceGC->WriteLogs: Timestamp generation has failed.");
                    return(false);
                }

                _LogGroupName  = BUtility.EncodeStringForTagging(_LogGroupName);
                _LogStreamName = BUtility.EncodeStringForTagging(_LogStreamName);

                string StreamIDBase = _LogGroupName + "-" + _LogStreamName + "-" + Timestamp;
                try
                {
                    var LogEntries = new LogEntry[_Messages.Count];

                    int i = 0;
                    foreach (var Message in _Messages)
                    {
                        LogEntries[i] = new LogEntry
                        {
                            LogName     = new LogName(ProjectID, StreamIDBase + "-" + (i + 1).ToString()).ToString(),
                            TextPayload = Message.Message
                        };

                        switch (Message.LogType)
                        {
                        case EBLoggingServiceLogType.Debug:
                            LogEntries[i].Severity = LogSeverity.Debug;
                            break;

                        case EBLoggingServiceLogType.Info:
                            LogEntries[i].Severity = LogSeverity.Info;
                            break;

                        case EBLoggingServiceLogType.Warning:
                            LogEntries[i].Severity = LogSeverity.Warning;
                            break;

                        case EBLoggingServiceLogType.Error:
                            LogEntries[i].Severity = LogSeverity.Error;
                            break;

                        case EBLoggingServiceLogType.Critical:
                            LogEntries[i].Severity = LogSeverity.Critical;
                            break;
                        }

                        i++;
                    }

                    LoggingServiceClient.WriteLogEntries(
                        LogNameOneof.From(new LogName(ProjectID, StreamIDBase)),
                        ResourceName,
                        new Dictionary <string, string>()
                    {
                        ["LogGroup"]  = _LogGroupName,
                        ["LogStream"] = _LogStreamName
                    },
                        LogEntries);

                    return(true);
                }
                catch (Exception e)
                {
                    _ErrorMessageAction?.Invoke("BLoggingServiceGC->WriteLogs: " + e.Message + ", Trace: " + e.StackTrace);
                }
            }
            return(false);
        }