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?

trajekolus
  • 535
  • 6
  • 16
  • you mean you need to remove `MySite.Pages.Customers[0]` from the log entry? – Chetan Jan 05 '22 at 02:14
  • @Chetan yes, that is what I want to not be logged. (I don't want to remove it afterwards) – trajekolus Jan 05 '22 at 02:18
  • This is something not available out of the box from Microsoft Logging extensions. You need to find your custom way out of it.. https://stackoverflow.com/questions/45015660/how-to-format-the-output-of-logs-in-the-consolemicrosoft-extensions-logging – Chetan Jan 05 '22 at 02: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 05 '22 at 02: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 05 '22 at 03:52
  • @Chetan Thanks, I ended up using Serilog instead, as I know it already – trajekolus Jan 05 '22 at 04:47

0 Answers0