Search code examples
azure-table-storageserilog

SeriLog Azure sync dropping }}?


Request logs with missing characters.

In Program.cs I set up Serilog using

    public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .UseStartup<Startup>().UseSerilog();

appsettings.json has

 "Serilog": {
   "Using": [ "Serilog.Sinks.AzureTableStorage" ],

My code logs a object object using

Using SeriLog
// etc
Log.Log.Information("REQUEST:" + Newtonsoft.Json.JsonConvert.SerializeObject(request));

When I query the log using

    public void GetData()
    {
        var info = new StorageInfo();
        var storageAccount = CloudStorageAccount.Parse(info.ConnectionString);
        var query = TableQuery.GenerateFilterConditionForDate("Timestamp", QueryComparisons.GreaterThanOrEqual,DateTimeOffset.Now.AddDays(-1).Date);
        var exQuery = new TableQuery<LogEntity>().Where(query);   
        var tableClient = storageAccount.CreateCloudTableClient();
        var table = tableClient.GetTableReference(info.TableName);
        var results = table.ExecuteQuery(exQuery);  
        foreach (LogEntity entity in results.OrderBy(x=>x.Timestamp))
        {
             var s =$"log: {entity.PartitionKey},  \t{entity.RenderedMessage},\t{entity.Timestamp},\t{entity.Timestamp.Millisecond} ms";
            Trace.WriteLine(s);
        }
    } 

However when I copy and paste the output into VSCode and format as Json I see that some closing curly brackets are missing


Solution

  • The main issue here is that you're not making request be a message-insert, as one generally should.

    Log.Log.Information("REQUEST:" + Newtonsoft.Json.JsonConvert.SerializeObject(request));
    

    should become:

    Log.Information("REQUEST: {request}", Newtonsoft.Json.JsonConvert.SerializeObject(request));
    

    I recommend reading https://serilog.net/ and the serilog wiki as it provides good background and examples.

    The Serilog Analyzer tool is very helpful for pointing things like this out on the fly as you edit in VS.