This article describes how to extend and configure log4net library to store log messages to Azure Document DB.
Download source code from here.
In order to extend log4net you should inherit AppenderSkeleton abstract class as follows:
In your config file:
Of course you should create Azure account before this and create DocumentDB, then you should copy endpoint and key to config file. DatabaseId and CollectionId doesn't matter, use any you want, they will be created in case not exists.
Download source code from here.
In order to extend log4net you should inherit AppenderSkeleton abstract class as follows:
| namespace log4net.Appender.Azure.DocumentDB | |
| { | |
| using Core; | |
| using Microsoft.Azure.Documents; | |
| using Microsoft.Azure.Documents.Client; | |
| using System; | |
| using System.Configuration; | |
| using System.Diagnostics; | |
| using System.Linq; | |
| using System.Threading.Tasks; | |
| public class DocumentDBAppender : AppenderSkeleton | |
| { | |
| private static DocumentClient client; | |
| private static readonly string databaseId = ConfigurationManager.AppSettings["DatabaseId"]; | |
| private static readonly string collectionId = ConfigurationManager.AppSettings["CollectionId"]; | |
| private static readonly string endpointUrl = ConfigurationManager.AppSettings["AccountEndpoint"]; | |
| private static readonly string authorizationKey = ConfigurationManager.AppSettings["AuthorizationKey"]; | |
| protected override void Append(LoggingEvent loggingEvent) | |
| { | |
| try | |
| { | |
| using (client = new DocumentClient(new Uri(endpointUrl), authorizationKey)) | |
| { | |
| var database = RetrieveOrCreateDatabaseAsync(databaseId).Result; | |
| var collection = RetrieveOrCreateCollectionAsync(database.SelfLink, collectionId).Result; | |
| var document = client.CreateDocumentAsync(collection.SelfLink, loggingEvent).Result; | |
| } | |
| } | |
| catch (DocumentClientException de) | |
| { | |
| Exception baseException = de.GetBaseException(); | |
| Debug.Print("Status code {0} error occurred: {1}, Message: {2}", de.StatusCode, de.Message, baseException.Message); | |
| } | |
| catch (Exception e) | |
| { | |
| Exception baseException = e.GetBaseException(); | |
| Debug.Print("Error: {0}, Message: {1}", e.Message, baseException.Message); | |
| } | |
| } | |
| private static async Task | |
| { | |
| // Try to retrieve the database (Microsoft.Azure.Documents.Database) whose Id is equal to databaseId | |
| var database = client.CreateDatabaseQuery().Where(db => db.Id == databaseId).AsEnumerable().FirstOrDefault(); | |
| // If the previous call didn't return a Database, it is necessary to create it | |
| if (database == null) | |
| { | |
| database = await client.CreateDatabaseAsync(new Database { Id = databaseId }); | |
| Debug.Print("Created Database: id - {0} and selfLink - {1}", database.Id, database.SelfLink); | |
| } | |
| return database; | |
| } | |
| private static async Task | |
| { | |
| // Try to retrieve the collection (Microsoft.Azure.Documents.DocumentCollection) whose Id is equal to collectionId | |
| var collection = client.CreateDocumentCollectionQuery(databaseSelfLink).Where(c => c.Id == id).ToArray().FirstOrDefault(); | |
| // If the previous call didn't return a Collection, it is necessary to create it | |
| if (collection == null) | |
| { | |
| collection = await client.CreateDocumentCollectionAsync(databaseSelfLink, new DocumentCollection { Id = id }); | |
| } | |
| return collection; | |
| } | |
| } | |
| } |
In your config file:
Of course you should create Azure account before this and create DocumentDB, then you should copy endpoint and key to config file. DatabaseId and CollectionId doesn't matter, use any you want, they will be created in case not exists.
Add these packages using NuGet:
Example of using
1. Create a console app in VS and use this configuration:
2. Implement Program.cs:
| namespace ConsoleApplication | |
| { | |
| using log4net; | |
| using log4net.Config; | |
| using System; | |
| using System.Threading.Tasks; | |
| class Program | |
| { | |
| static void Main(string[] args) | |
| { | |
| Task task = Task.Factory.StartNew(() => | |
| { | |
| try | |
| { | |
| BasicConfigurator.Configure(); | |
| log4net.Util.LogLog.InternalDebugging = true; | |
| throw new FormatException("328742093529734203984120-49723"); | |
| } | |
| catch (Exception ex) | |
| { | |
| LogManager.GetLogger(typeof(Program)).Error("ConsoleApplication.Main", ex); | |
| } | |
| }); | |
| task.Wait(); | |
| Console.ReadLine(); | |
| } | |
| } | |
| } |



Комментариев нет:
Отправить комментарий