.NET logging to a database: Create a custom provider with ILogger (uses .NET Core)

preview_player
Показать описание
Using .NET logging to log to a database is possible by creating a custom logging provider using ILogger.

This is great if you're building a .NET Core application.

Creating a logging provider is relatively easy in ASP.NET Core as you can build the functionality around methods that are contained in the ILogger and ILoggerProvider interfaces.

It will walk you through how to configure and implement database logging through an ASP.NET Core app. There is functionality available to configure error handling on particular assemblies and namespaces.

As we are writing the log in JSON format, we'll show you how to query JSON strings if you're using SQL Server 2017, or above.

💻 Download the code example for this demo 💻

🎥 How ASP.NET Core logging works and use ILogger and ILoggerProvider to write a log 🎥

🎥 Create Your Own Logging Provider to Log to Text Files in .NET Core 🎥

📖 Learn .NET and C# with our online courses 📖

► Chapters
0:00 Coming up...
0:11 How to create a custom logging provider
0:54 Code walkthrough
4:24 Code demo
4:40 JSON in SQL Server
5:41 Other custom logging providers

#ilogger #aspnetcore #sqlserver
Рекомендации по теме
Комментарии
Автор

This is exactly what i was missing i was getting lock behind a infinite loop on creating the logging, and now Ive manage the missing pieces on my side thank you very much!

atuuuuum
Автор

Hi, I don t see where in your code the log method is called ?

foudilbenouci
Автор

how to implement the same using Entity framework ? Any samples available? Passing DBcontext as argument in program.cs throws error.. please help

swethashibin
Автор

Hey David, thank you so much for the amazing effort (and blog article which led me here).

I have this very similar implemented in my case, but was wondering if I can add non-blocking execution so was thinking a non awaited: _ = Task.Run(() => the insertion code here), what do you think, does it make sense? Also, why dispatch each log message, why not batch it? Unfortunately I am super unsure of the lifecycle of these loggers so was thinking using a MemoryCache.Default basically as a buffer, ideas?

vekzdran
Автор

How about .NET 5? What to do in program.cs/startup.cs for configuration?

yvheng
Автор

what about those who have not moved yet to .net core?

burreifort
Автор

Hi! preserveLogFilename does not work in net.core, I want to keep the logfile name the same and when reached sizelimit create a new file with postfix, as below
log_filename.log CURRENT (Always same name)
log_filename.log.1 OLD File

ahmadjaber