2

I have added logging to two projects (.net-5 and .net-6) using Microsoft.Extensions.Logging SimpleConsole as below:

services.AddLogging(builder => builder
                                .AddSimpleConsole(options =>
                                {
                                    options.IncludeScopes = true;
                                    options.SingleLine = true;
                                    options.TimestampFormat = "yyyy-MM-dd hh:mm:ss ";
                                }));

But when I log my own messages using:

_logger.LogInformation($"customer: {selectedCustomer.Name}");

the section following "info:" contains the Log Category in front of the message I want to log.

2022-01-05 10:13:17 info: MySite.Pages.Customers[0] customer: admin

The messages I log can be long, and I would still like to see them on one line that fits into the screen. Therefore, how can I get rid of the Log Category, so it it logs only my message?

6
  • you mean you need to remove MySite.Pages.Customers[0] from the log entry?
    – Chetan
    Jan 5, 2022 at 2:14
  • @Chetan yes, that is what I want to not be logged. (I don't want to remove it afterwards)
    – trajekolus
    Jan 5, 2022 at 2:18
  • This is something not available out of the box from Microsoft Logging extensions. You need to find your custom way out of it.. stackoverflow.com/questions/45015660/…
    – Chetan
    Jan 5, 2022 at 2:43
  • @Chetan I saw that question too, but Microsoft Logging has progressed since 2017 with oneliners now being easy, for example. So I was hoping that this too could be configured.
    – trajekolus
    Jan 5, 2022 at 2:48
  • You can use log4net logging extensions of Microsoft (if you are developing asp.net core application) logging and use pattern layout feature of log4net to format the logentries as per your requirement.
    – Chetan
    Jan 5, 2022 at 3:52

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.