question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Custom Formatting

See original GitHub issue

I am using Serilog HTTP (5.0.1 version) sink for logging to Logstash in my .Net Core Project. In startup.cs I have following code to enable serilog.

Log.Logger = new LoggerConfiguration() .Enrich.FromLogContext() .WriteTo.Http("http://mylogstashhost.com:5000").Enrich.WithProperty("user", "xxx").Enrich.WithProperty("serviceName", "yyy") .MinimumLevel.Warning() .CreateLogger();

My events are being send as the following format.

{"events":[{"Timestamp":"2018-10-19T18:16:27.6561159+01:00","Level":"Warning","MessageTemplate":"abc","RenderedMessage":"abc","user":"xxx","serviceName":"yyy","Properties":{"ActionId":"b313b8ed-0baf-4d75-a6e2-f0dbcb941f67","ActionName":"MyProject.Controllers.HomeController.Index","RequestId":"0HLHLQMV1EBCJ:00000003","RequestPath":"/"}}]}

But seems receiving Logstash endpoint does not accept such format and I can not see my logs on Kibana. But when I send as following format all is ok.

{"Timestamp":"2018-10-19T18:16:27.6561159+01:00","Level":"Warning","MessageTemplate":"abc","RenderedMessage":"abc","user":"xxx","serviceName":"yyy","Properties":{"ActionId":"b313b8ed-0baf-4d75-a6e2-f0dbcb941f67","ActionName":"MyProject.Controllers.HomeController.Index" ,"RequestId":"0HLHLQMV1EBCJ:00000003","RequestPath":"/"}}

So Logstash doesnt like the event to be in Events{} and also it wants “user” and “ServiceName” tags out of “Properties”. I read posts about custom formatting, tried to use CompactJsonFormatter, JsonFormatter but none of them gave me what i wanted. Is there a way to achieve this ?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
FantasticFiascocommented, Oct 20, 2018

Very good, let me know if you encounter any other problems.

0reactions
ismail-tellioglucommented, Oct 20, 2018

Thanks for your help. I could achieve the format i need, by modifying ArrayBatchFormatter a little:

`public class MyFormat : BatchFormatter { public MyFormat(long? eventBodyLimitBytes = 256 * 1024): base(eventBodyLimitBytes) {

    }
    public override void Format(IEnumerable<string> logEvents, TextWriter output)
    {
        if (logEvents == null) throw new ArgumentNullException(nameof(logEvents));
        if (output == null) throw new ArgumentNullException(nameof(output));

        // Abort if sequence of log events is empty
        if (!logEvents.Any())
        {
            return;
        }

        output.Write("[");

        var delimStart = string.Empty;

        foreach (var logEvent in logEvents)
        {
            if (string.IsNullOrWhiteSpace(logEvent))
            {
                continue;
            }
            int index = logEvent.IndexOf("{");

            string adjustedString = "{\"user\":\"xxx\",\"serviceName\" : \"yyy\"," + logEvent.Substring(1);
            if (CheckEventBodySize(adjustedString))
            {
                output.Write(delimStart);
                output.Write(adjustedString);
                delimStart = ",";
            }
        }

        output.Write("]");
    }
}`
Read more comments on GitHub >

github_iconTop Results From Across the Web

Create and apply a custom number format
Create a custom number format · On the Home tab, under Number, on the Number Format pop-up menu · In the Format Cells...
Read more >
How to Use Custom Formatting in Excel
In the Home tab, go to the Number section and click on General. · From the menu, select More Number Formats. · In...
Read more >
7 Amazing Excel Custom Number Format Tricks (you Must ...
Excel Custom Number formatting is the clothing for data in excel cells. You can dress these the way you want. All you need...
Read more >
Excel custom number formats
Custom number formats can control the display of numbers, dates, times, fractions, percentages, and other numeric values. Using custom formats, you can do ......
Read more >
The Definitive Guide to Custom Number Formats in Excel
Number formatting in Excel is pretty powerful but that means it is also somewhat complex. This is the definitive guide to Excel's custom...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found