Infrastructure: Improve experiece for filtering log data
See original GitHub issueAt 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 andMicrosoft.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:
- Created 7 years ago
- Comments:6 (3 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@Ponant you can do that by calling the formatter, same as the docs. I just thought you were specifically after the command text 😄
@demodav This is old code used to describe an issue–it is not intended to run on recent releases.