question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Infrastructure: Improve experiece for filtering log data

See original GitHub issue

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;
            }
        }
    }

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
rowanmillercommented, Dec 8, 2016

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

0reactions
ajcvickerscommented, Aug 30, 2018

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

Log Patterns and Drop Filters | New Relic
Learn how to use log patterns and drop filters to drop unwanted log attributes, so you can focus on critical log data within...
Read more >
Log management best practices for modern applications ...
Log management best practices for your modern apps and infrastructure ... Log data as a plain text file can't be queried or filtered...
Read more >
A Guide to Log Filtering: Tips for IT Pros
In this post, I'll show how you can filter log messages and make them useful for solving your application and infrastructure problems.
Read more >
What is Log Management? 4 Best Practices & More
Indexing or Search: A log management tool that helps the IT organization filter, sort, analyze or search data across all logs.
Read more >
Creating metrics from log events using filters
Metric filters define the terms and patterns to look for in log data as it is sent to CloudWatch Logs. CloudWatch Logs uses...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found