Skip to content

Infrastructure: Improve experiece for filtering log data #7217

Closed

Description

@rowanmiller

At the moment you need to filter by category name, which has some issues:

  • It's hard to work out the full set of categories you want
  • You are potentially referencing internal types
  • Type names can change (i.e. you want Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommandBuilderFactory in 1.0 and Microsoft.EntityFrameworkCore.Storage.IRelationalCommandBuilderFactory in 1.1)

Even once you do this, it is hard to filter to the events that you want. Here is an attempt to filter to commands being executed, but EventIds are not globally unique... so there is no guarantee that I just get RelationalEventId.ExecutedCommand events. Even in this example, where I am just logging for one component, there is no guarantee that the command builder factory won't log something related to the EventId in core that collides with the id of RelationalEventId.ExecutedCommand.

The types we pass to state also make is pretty cumbersome to get the data I want (command text).

    public class SqlLoggerProvider : ILoggerProvider
    {
        public ILogger CreateLogger(string categoryName)
        {
            if (categoryName == typeof(Microsoft.EntityFrameworkCore.Storage.IRelationalCommandBuilderFactory).FullName)
            {
                return new SqlLogger(categoryName);
            }

            return new NullLogger();
        }

        public void Dispose()
        { }

        private class SqlLogger : ILogger
        {
            private string _test;

            public SqlLogger(string test)
            {
                _test = test;
            }

            public bool IsEnabled(LogLevel logLevel)
            {
                return true;
            }

            public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter)
            {
                if (eventId.Id == (int)RelationalEventId.ExecutedCommand)
                {
                    var data = state as IEnumerable<KeyValuePair<string, object>>;
                    if (data != null)
                    {
                        var commandText = data.Single(p => p.Key == "CommandText").Value;
                        Console.WriteLine(commandText);
                    }
                }
            }

            public IDisposable BeginScope<TState>(TState state)
            {
                return null;
            }
        }

        private class NullLogger : ILogger
        {
            public bool IsEnabled(LogLevel logLevel)
            {
                return false;
            }

            public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter)
            { }

            public IDisposable BeginScope<TState>(TState state)
            {
                return null;
            }
        }
    }

Activity

  1. rowanmiller commented on Dec 8, 2016

    @rowanmiller
    ContributorAuthor

    BTW the quickest win would be to set EventId.Name to the name of the enum member (currently they are all null).

  2. Ponant commented on Dec 8, 2016

    @Ponant

    @rowanmiller I think the example you gave in the docs has the advantage of returning more info, e.g. execution time.

  3. rowanmiller commented on Dec 8, 2016

    @rowanmiller
    ContributorAuthor

    @Ponant you can do that by calling the formatter, same as the docs. I just thought you were specifically after the command text 😄

  4. Ponant commented on Dec 12, 2016

    @Ponant

    Yes, you are right. At least I learned that the exec command is not part of the command text. In any case I implemented your code above and will follow up this issue. Cheers

  5. added this to the 2.0.0 milestone on Jan 3, 2017
  6. changed the title [-]Improve experiece for filtering log data[/-] [+]Infrastructure: Improve experiece for filtering log data[/+] on May 9, 2017
  7. demodav commented on Aug 30, 2018

    @demodav

    How does one use this code? How do you execute it please provide an example.

  8. ajcvickers commented on Aug 30, 2018

    @ajcvickers
    Contributor

    @demodav This is old code used to describe an issue--it is not intended to run on recent releases.

  9. modified the milestones: , 2.0.0 on Oct 15, 2022
  10. removed their assignment
    on Sep 1, 2024
  11. added theissue type on Jun 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions