Example #1
0
        public async void Log(LogEntry logEntry)
        {
            try
            {
                string jsonLogEntry;
                try
                {
                    jsonLogEntry = JsonConvert.SerializeObject(logEntry, settings);
                }
                catch (Exception e)
                {
                    logEntry = new LogEntry()
                    {
                        Exception       = e,
                        Level           = "Warning",
                        MessageTemplate = "Log Serialization failed with exception",
                        Timestamp       = DateTime.UtcNow,
                        EventId         = new EventId(7437)
                    };
                    jsonLogEntry = JsonConvert.SerializeObject(logEntry, settings);
                }

                await eventHubClientWrapper.SendAsync(new EventData(Encoding.UTF8.GetBytes(jsonLogEntry)));
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
        }
Example #2
0
        public async void Log(LogEntry logEntry)
        {
            try
            {
                string jsonLogEntry;
                try
                {
                    jsonLogEntry = JsonConvert.SerializeObject(logEntry, settings);
                }
                catch (Exception e)
                {
                    logEntry = new LogEntry()
                    {
                        Exception       = e,
                        Level           = "Warning",
                        MessageTemplate = "Log Serialization failed with exception",
                        Timestamp       = DateTime.UtcNow,
                        EventId         = new EventId(7437)
                    };
                    jsonLogEntry = JsonConvert.SerializeObject(logEntry, settings);
                }

                AzureStorageLoggingCheckResult azureStorageLoggingCheckResult = this.azureStorageBlobContainerBuilder.NeedsAzureStorageLogging(jsonLogEntry, 1);

                switch (azureStorageLoggingCheckResult)
                {
                case AzureStorageLoggingCheckResult.LogWarningNoStorage:
                    jsonLogEntry = logEntry.ToLongMessageWarning(settings);
                    break;

                case AzureStorageLoggingCheckResult.LogWarningAndStoreMessage:
                    var    azureLogger  = new AzureStorageEventLogger(this.azureStorageBlobContainerBuilder.BlobContainerClient);
                    string blobFullName = azureLogger.GenerateBlobFullName(
                        new AzureStorageBlobFullNameModel(azureLogger.GenerateServiceName(
                                                              logEntry.LogProperties.GetLogEntryPropertyValue("_Service"),
                                                              logEntry.LogProperties.GetLogEntryPropertyValue("_Environment")),
                                                          azureLogger.GeneratePathForErrorBlob(logEntry.Timestamp),
                                                          azureLogger.GenerateErrorBlobName()));
                    var azureStorageModel = new AzureStorageEventModel(blobFullName, jsonLogEntry);
                    var result            = await azureLogger.StoreLogFileAsync(azureStorageModel);

                    jsonLogEntry = result.ToJsonLogEntryString(this.azureStorageBlobContainerBuilder.AzureStorageLogProviderOptions, logEntry, settings);
                    break;
                }

                await eventHubClientWrapper.SendAsync(new EventData(Encoding.UTF8.GetBytes(jsonLogEntry)));
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
        }