© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•15mo ago•
13 replies
TheBrambleShark

Custom Logger

Hi all, been trying to figure this out for a bit now but I'm not sure what's happening. I am writing a custom extension for Microsoft.Extensions.Logging to write to a Sql Server database with a particular schema. However, I'd opted to make it as generic as possible.

After doing a lot of research, I've created a DbLoggingProvider and a MSSqlDatabaseLogger. The logging provider creates new instances of the database logger, as expected, and the database logger expects an instance of a type implementing
ILogAgent
ILogAgent
which it forwards its log method to. The log agent is responsible for performing the database-specific operations.

However, when doing unit tests, I am unable to prove that logging works. In fact, it appears to not. Take the following test.
[Fact]
public void LogsToDatabase()
{
    using var accessor = new SqlServerAccesor(_connectionString); // SqlServerAccessor wraps a SqlConnection and exposes helpers to run queries and non-queries.

    int? oldCount = GetCount(accessor); // Uses the accessor to run a query to check count of rows in a table.
    Assert.NotNull(oldCount);

    var logger = _host.Services.GetService<ILogger<LoggerTests>>();
    Assert.NotNull(logger);

    var exception = Record.Exception(() => logger.LogInformation("..."));
    Assert.Null(exception);

    int? newCount = GetCount(accessor);
    Assert.NotNull(newCount);
    Assert.NotEqual(oldCount, newCount);
}
[Fact]
public void LogsToDatabase()
{
    using var accessor = new SqlServerAccesor(_connectionString); // SqlServerAccessor wraps a SqlConnection and exposes helpers to run queries and non-queries.

    int? oldCount = GetCount(accessor); // Uses the accessor to run a query to check count of rows in a table.
    Assert.NotNull(oldCount);

    var logger = _host.Services.GetService<ILogger<LoggerTests>>();
    Assert.NotNull(logger);

    var exception = Record.Exception(() => logger.LogInformation("..."));
    Assert.Null(exception);

    int? newCount = GetCount(accessor);
    Assert.NotNull(newCount);
    Assert.NotEqual(oldCount, newCount);
}


I'm sure that there are better ways to test if a logger is logging (this could in theory pass if something else is writing logs to this table even if the log operation fails), but it's fine for right now while I'm the only person logging to this table. That said, advice on fixing up this test would be appreciated.

Every assert passes except for the assertion that the counts are not equal (they are) and going to the database reveals that my message was not inserted into the log table. I have unit tests that pass for SqlServerAccessor and I am able to demonstrate that, by itself, it is able to insert log entries by executing a non-query and writing directly to the table in question.

The issue appears to be in the glue layer - despite the log agent being present in the same service provider which is producing logger instances, it does not appear to be using my particular logger. I don't see any breakpoints being hit in my logger class, though I am hitting the logger provider several times. What is it exactly that I am missing here?
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources
Was this page helpful?

Similar Threads

Recent Announcements

Similar Threads

AspNetCore - Logger - Global Scope
C#CC# / help
2y ago
LoggerMessage organisation
C#CC# / help
2y ago
❔ Fire and forget, do I need a new logger?
C#CC# / help
3y ago
✅ Custom class arrays, parameters, custom methods.
C#CC# / help
3y ago