Log4Net user does not create table or saves records correctly

I am currently creating a prototype of a custom Log4Net application that will store information about all exceptions that occur in a project in an Azure table. The table is created based on the model defined in the "LogEntry" class. Since this is a prototype web application, at the moment I have created a button that throws an exception to start the registrar, and I am following this as a guide:

http://www.kongsli.net/nblog/2011/08/15/log4net-writing-log-entries-to-azure-table-storage/

However, when an exception is thrown and a log is created, the table is not created correctly. Instead of creating a table based on my LogEntry class, it only generates what I assume as the default values ​​for the TableServiceContext for "PartitionKey", "RowKey" and "TimeStamp". As a result, the logger does not execute any records in the table.

Below are some excerpts from my project:

LogEntry.cs

public class LogEntry : TableServiceEntity { public LogEntry() { var now = DateTime.UtcNow; // PartitionKey is the current year and month whild RowKey is a combination of the date, time and a GUID. // This is so that we are able to query our log entries more efficiently. PartitionKey = string.Format("{0:yyyy-MM}", now); RowKey = string.Format("{0:dd HH:mm:ss.fff}-{1}", now, Guid.NewGuid()); } // This region of the class class represents each entry in our log table. #region Table Columns ...all columns defined here... #endregion } 

LogServiceContext.cs

 internal class LogServiceContext : TableServiceContext { public LogServiceContext(string baseAddress, StorageCredentials credentials) : base(baseAddress, credentials) { } internal void Log(LogEntry logEntry) { AddObject("LogEntries", logEntry); SaveChanges(); } public IQueryable<LogEntry> LogEntries { get { return CreateQuery<LogEntry>("LogEntries"); } } } 

And extracting from the appender class itself:

 // Create a new LogEntry and store all necessary details. // All writing to log is done asynchronically to prevent the write slowing down request handling. Action doWriteToLog = () => { try { _ctx.Log(new LogEntry { CreatedDateTime = DateTime.Now, UserName = loggingEvent.UserName, IPAddress = userIPAddress, Culture = userCulture, OperatingSystem = userOperatingSystem, BrowserVersion = userCulture, ExceptionLevel = loggingEvent.Level, ExceptionDateTime = loggingEvent.TimeStamp, ExceptionMessage = loggingEvent.RenderedMessage, ExceptionStacktrace = Environment.StackTrace, AdditionalInformation = loggingEvent.RenderedMessage }); } catch (DataServiceRequestException e) { ErrorHandler.Error(string.Format("{0}: Could not wring log entry to {1}: {2}", GetType().AssemblyQualifiedName, _tableEndpoint, e.Message)); } }; doWriteToLog.BeginInvoke(null, null); 

I am happy to provide any additional information and I can package the solution if someone wants to see the classes in full form. Any help would be greatly appreciated!

+4
source share
1 answer

after writing the blog post you are linking to, I made some minor changes to the code. You can see the change in my github registry: https://github.com/vidarkongsli/azuretablestorageappender

Essentially, what I did was replace SaveChanges () with BeginSaveChanges (SaveChangesOptions.Batch, null, null) and remove the BeginInvoke statement from AzureTableStorageAppender.Append (LoggingEvent)

I think this can help the situation.

0
source

Source: https://habr.com/ru/post/1484198/


All Articles