The .NET Stacks, #60: 📝Logging improvements in .NET 6
Tags: csharp, dotnet, azure
Published: 2021-08-08 23:54:31

---
title: The .NET Stacks, #60: 📝Logging improvements in .NET 6
published: true
date: 2021-08-08 17:14:00 UTC
tags: csharp, dotnet, azure
canonical_url: https://www.daveabrock.com/2021/08/08/dotnet-stacks-60/
---

Happy Monday! It's hard to believe it's August already. What are we talking about this week?

- **One big thing**: Logging improvements in .NET 6
- **The little things**: Notes from the community
- Last week in the .NET world

* * *

## One big thing: Logging improvements in .NET 6

Over the last few months, we've highlighted some big features coming with .NET 6. Inevitably, there are some features that fly under the radar, including various logging improvements that are coming. Let's talk about a few of them.

Announced with Preview 4 at the end of May, .NET will [include a new Microsoft.Extensions.Logging compile-time source generator](https://devblogs.microsoft.com/dotnet/announcing-net-6-preview-4/#microsoft-extensions-logging-compile-time-source-generator). The `Microsoft.Extensions.Logging` namespace will expose a `LoggerMessageAttribute` type that generates "performant logging APIs" at compile-time. (The source code relies on `ILogger`, thankfully.)

From the blog post:

> The source generator is triggered when `LoggerMessageAttribute` is used on `partial` logging methods. When triggered, it is either able to autogenerate the implementation of the `partial` methods it’s decorating, or produce compile-time diagnostics with hints about proper usage. The compile-time logging solution is typically considerably faster at run time than existing logging approaches. It achieves this by eliminating boxing, temporary allocations, and copies to the maximum extent possible.

Rather than using `LoggerMessage` APIs directly, the .NET team says this approach offers simpler syntax with attributes, the ability to offer warnings as a "guided experience," support for more logging parameters, and more.

As a quick example, let's say I can't talk to an endpoint. I'd configure a `Log` class like this:

```csharp
public static partial class Log
{
    [LoggerMessage(EventId = 0, Level = LogLevel.Critical, Message = "Could not reach `{uri}`")]
    public static partial void CouldNotOpenSocket(ILogger logger, string uri);
}
```

There is much, much more at the Microsoft doc, _[Compile-time logging source generation](https://docs.microsoft.com/en-us/dotnet/core/extensions/logger-message-generator)_.

On the topic of HTTP requests, ASP.NET Core has a couple of new logging improvements worth checking out, too. A big one is the introduction of HTTP logging middleware, which can log information about HTTP requests and responses, including the headers and body. According [to the documentation](https://docs.microsoft.com/en-us/aspnet/core/fundamentals/http-logging/?view=aspnetcore-6.0), the middleware logs common properties like path, query, status codes, and headers with a call to `LogLevel.Information`. The HTTP logging middleware is quite configurable: you can filter logging fields, headers to log (you need to explicitly define the headers to include), media type options, and a log limit for the response body.

This addresses a common use case for web developers. As always, you'll need to keep an eye on possible performance impacts and mask logging sensitive information (like PII data).

Lastly, with .NET 6 Preview 5 the .NET team [rolled out subcategories for Kestrel log filtering](https://devblogs.microsoft.com/aspnet/asp-net-core-updates-in-net-6-preview-5/#subcategories-for-better-filtering-of-kestrel-logs). Previously, verbose Kestrel logging was quite expensive as all of Kestrel has shared the same category name. There are new categories on top of `*.Server.Kestrel`, including `BadRequests`, `Connections`, `Http2`, and `Http3`. This allows you to be more selective on which rules you want to enable. As shown in the blog post, if you only want to filter on bad requests, it's as easy as this:

```json
{
  "Logging": {
    "LogLevel": {
      "Microsoft.AspNetCore.Kestrel.BadRequests": "Debug"
    }
  }
}
```

If you want to learn more about ASP.NET Core logging improvements in .NET 6, Maryam Ariyan and Sourabh Shirhatti will be [talking about it in Tuesday's ASP.NET standup](https://www.youtube.com/watch?v=V-rwe2yxh1A).

* * *

## The little things: Notes from the community

AutoMapper is a powerful [but self-proclaimed "simple little library"](https://automapper.org/) for mapping objects. From [Shawn Wildermuth on Twitter](https://twitter.com/ShawnWildermuth/status/1421863407830118408), Dean Dashwood has written about [_Three Things I Wish I Knew About AutoMapper Before Starting_](https://github.com/ddashwood/AutoMapperBlog/blob/main/AutoMapper%20Blog.md). It centers around subtleties when working with dependency injection in models, saving changed and nested data, and changing the models. It's actually a GitHub repo with a writeup and some code samples—highly recommended.

* * *

Sometimes one sentence does the trick: [Cezary Piątek has a nice MsBuild cheatsheet on GitHub](https://github.com/cezarypiatek/msbuild-cheatsheet).

* * *

This week, Mike Hadlow wrote about [creating a standalone](https://mikehadlow.com/posts/standalone-consoleloggerprovider/)`ConsoleLoggerProvider`[in C#](https://mikehadlow.com/posts/standalone-consoleloggerprovider/). If you want to create a standalone provider to avoid working with dependency injection and hosting, Mike found an ... interesting ... design. He develops a simple no-op implementation.

* * *

Well, it's been 20 years since the Agile Manifesto, and Al Tenhundfeld celebrates it with the article [_Agile at 20: The Failed Rebellion_](https://www.simplethread.com/agile-at-20-the-failed-rebellion/). I think we can all agree that (1) everybody wants to claim they are Agile, and (2) Agile is a very, um, flexible word—nobody is really _doing_ Agile (at least according to the Manifesto). He explores why; an interesting read.

* * *

## 🌎 Last week in the .NET world

### 🔥 The Top 3

- Oren Eini [writes that System.Threading.Tasks.Task isn’t an execution unit](https://ayende.com/blog/194337-B/system-threading-tasks-task-isnt-an-execution-unit?Key=885fdaff-cc07-4ab1-adad-c278fe7de264).
- Andrew Lock [continues his deep dive on StringBuilder](https://andrewlock.net/a-deep-dive-on-stringbuilder-part-3-converting-chunks-to-a-string-with-tostring/), and [so does Steve Gordon](https://www.stevejgordon.co.uk/how-does-the-stringbuilder-work-in-net-part-3-how-appending-works-and-the-stringbuilder-expands).
- Al Tenhundfeld [speaks the truth on Agile](https://www.simplethread.com/agile-at-20-the-failed-rebellion/).

### 📢 Announcements

- Dapr v1.3 [is now available](https://blog.dapr.io/posts/2021/07/27/dapr-v1.3-is-now-available/).
- Michael Hawker [introduces the Community Toolkit](https://devblogs.microsoft.com/ifdef-windows/welcome-to-the-community-toolkit).
- Uno Platform [shows off improvements with 3.9](https://platform.uno/blog/uno-platform-3-9-net-6-mobile-vs-2022-winui-plus-xaml-trimming-for-50-wasm-aot-size-reduction/).

### 📅 Community and events

- Jeremy Miller [writes about Marten, the generic host builder in .Net Core, and more](https://jeremydmiller.com/2021/07/29/marten-the-generic-host-builder-in-net-core-and-why-this-could-be-the-golden-age-for-oss-in-net/).
- Mike Brind [is writing a book about Razor Pages](https://www.mikesdotnetting.com/article/356/i-am-writing-a-book-about-razor-pages).
- Sha Ma [writes about GitHub's journey from monoliths to microservices](https://www.infoq.com/articles/github-monolith-microservices/).
- Richard Lander [posts a discussion about the .NET open source project](https://devblogs.microsoft.com/dotnet/conversation-about-the-net-open-source-project).
- Matthew MacDonald asks: [what if GitHub Copilot worked like a real programmer?](https://medium.com/young-coder/what-if-github-copilot-worked-like-a-real-programmer-337581375aae)
- The .NET Docs Show [talks to Vahid Farahmandian about gRPC and .NET 5](https://www.youtube.com/watch?v=m6KXIk7S6GE).
- For community standups: ASP.NET [talks to Isaac Abraham about F#](https://www.youtube.com/watch?v=JuIq7mU50jA), and Entity Framework [talks about OData](https://www.youtube.com/watch?v=Q3Ove-2Uh94).
- Check out all the [Focus on F# sessions on YouTube](https://www.youtube.com/watch?v=i3qEhwcG7ps).

### 🌎 Web development

- The Code Maze blog [writes about Onion Architecture in ASP.NET Core](https://code-maze.com/onion-architecture-in-aspnetcore/).
- Damien Bowden [secures ASP.NET Core Razor Pages and Web APIs with Azure B2C External and Azure AD internal identities](https://damienbod.com/2021/07/26/securing-asp-net-core-razor-pages-web-apis-with-azure-b2c-external-and-azure-ad-internal-identities/).

### 🥅 The .NET platform

- Mike Hadlow [creates a standalone ConsoleLoggerProvider](https://mikehadlow.com/posts/standalone-consoleloggerprovider/).
- Santosh Hari [uses app secrets in .NET Core console apps](https://santoshhari.wordpress.com/2021/07/26/using-app-secrets-in-dotnetcore-console-applications/).

### ⛅ The cloud

- April Edwards [compares Azure Static Web Apps, Azure WebApps, and Azure Blob Storage Static Sites](https://devblogs.microsoft.com/devops/comparing-azure-static-web-apps-vs-azure-webapps-vs-azure-blob-storage-static-sites).
- Mark Heath [writes about the serverless sliding scale](https://markheath.net/post/serverless-sliding-scale).
- John Kilmister [gets started with serverless SignalR and Azure Functions](https://www.blueboxes.co.uk/getting-started-with-serverless-signalr-and-azure-functions).
- Paul Michaels [configures Cloudflare to work with Azure domains](https://www.pmichaels.net/2021/07/24/configure-cloudflare-to-work-with-your-azure-domain-registration/).

### 📔 Languages

- Anthony Giretti [writes about extended property patterns in C# 10](https://anthonygiretti.com/2021/07/27/introducing-c-10extended-property-patterns/).
- Gergely Sinka [toggles functionality in C# with feature flags](https://developer.okta.com/blog/2021/07/28/toggle-feature-flags-csharp).

### 🔧 Tools

- David Ramel [writes about Entity Framework Core 6](https://visualstudiomagazine.com/articles/2021/07/28/efcore6-preview6.aspx).
- Rachel Appel [writes about Blazor debugging improvements in Rider 2021.2](https://blog.jetbrains.com/dotnet/2021/07/27/blazor-debugging-improvements-in-rider-2021-2/).
- Khalid Abuhakmeh [writes an extension method that parses a redis-cli connection string for the StackExchange.Redis library](https://khalidabuhakmeh.com/redis-cli-connection-uri-with-stackexchangeredis).
- Dmitry Lyalin [writes about hot reload in Visual Studio 2022](https://devblogs.microsoft.com/visualstudio/speed-up-your-dotnet-and-cplusplus-development-with-hot-reload-in-visual-studio-2022).
- Kat Cosgrove [writes about Kubernetes fundamentals](https://www.pulumi.com/blog/kubernetes-fundamentals-part-one/).
- Matthew Jones [creates a Dapper helper C# class to generate parameterized SQL](https://exceptionnotfound.net/creating-a-dapper-helper-csharp-class-to-generate-parameterized-sql-inserts-and-updates/).

### 📱 Xamarin

- Leomaris Reyes [reproduces a fashion UI in Xamarin](https://askxammy.com/replicating-fashion-ui-in-xamarin-forms/).
- Charlin Agramonte [creates an expandable paragraph control in Xamarin](https://xamgirl.com/expandable-paragraph-control-in-xamarin-forms/).

### 🏗 Design, testing, and best practices

- Oren Eini [writes about flexible business systems](https://ayende.com/blog/194274-B/responsibility-abdication-the-path-to-create-flexible-business-system?Key=f7efcebb-71e0-4050-868b-c8df437cd574).
- Derek Comartin [uses a message-driven architecture to decouple a monolith](https://codeopinion.com/message-driven-architecture-to-decouple-a-monolith/).
- Charles Humble [writes about the future of microservices](https://thenewstack.io/the-future-of-microservices-more-abstractions/).
- Suzanne Scacca [uses heatmaps to audit web design](https://www.telerik.com/blogs/tips-using-heatmaps-audit-web-design).
- Davide Bellone [explores the tradeoffs of performance and clean code](https://www.code4it.dev/blog/clean-code-vs-performance).
- Ben Nadel [writes about feature flags](https://www.bennadel.com/blog/4089-feature-flags-shift-the-balance-of-power-away-from-designers.htm).
- Jimmy Bogard [continues his series on domain-driven refactoring](https://jimmybogard.com/domain-driven-refactoring-extracting-domain-services/).
- Ian Cartwright, Rob Horn, & James Lewis [write about feature parity](https://martinfowler.com/articles/patterns-legacy-displacement/feature-parity.html).
- InfoQ talks to [Michael Perry about immutable architecture, the CAP theorem, and CRDTs](https://www.infoq.com/podcasts/michael-perry-crdts-art-of-immutable-architecture/).

### 🎤 Podcasts

- The 6-Figure Developer Podcast [talks to Bryan Hogan about Polly](https://6figuredev.com/podcast/episode-205-polly-with-bryan-hogan/).
- Adventures in .NET [talks about the global reach of .NET](https://devchat.tv/adventures-in-dotnet/global-reach-of-net-net-079/).
- The Merge Conflict podcast [talks about FOSS and .NET MAUI Web with Ooui](https://www.mergeconflict.fm/264).
- The .NET Core Podcast [discusses Gremlinq With Daniel Weber](https://dotnetcore.show/episode-81-gremlinq-with-daniel-weber/).
- The Azure DevOps Podcast [talks about what's coming for developers](http://azuredevopspodcast.clear-measure.com/amanda-silver-on-whats-coming-for-developers-episode-151).
- Scott Hanselman [talks to Don Syme about F#](https://www.hanselminutes.com/799/learn-f-to-write-succinct-performant-and-correct-code-with-don-syme).
- The Azure Podcast [talks about Azure Arc](http://azpodcast.azurewebsites.net/post/Episode-387-Azure-Arc).

### 🎥 Videos

- The ASP.NET Monsters [works on accessibility testing with Playwright and Axe](https://www.youtube.com/watch?v=m2ouoDx8_wI).
- Luis Quintanilla [rolls out a nice "intro to F#" video series](https://www.youtube.com/watch?v=yGzu0iDuMNQ).
- Shawn Wildermuth [talks about changes coming to Startup in .NET 6](https://wildermuth.com/2021/07/25/Coding-Shorts-Coding-Shorts-Changes-to-Coming-to-Startup-in-NET-6).
- On .NET talks [about ValueTask in C#](https://channel9.msdn.com/Shows/On-NET/C-Language-Highlights-ValueTask).