Repository navigation
Closed
Milestone
Description
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.RelationalCommandBuilderFactoryin 1.0 andMicrosoft.EntityFrameworkCore.Storage.IRelationalCommandBuilderFactoryin 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
rowanmiller commentedon Dec 8, 2016
BTW the quickest win would be to set EventId.Name to the name of the enum member (currently they are all null).
Ponant commentedon Dec 8, 2016
@rowanmiller I think the example you gave in the docs has the advantage of returning more info, e.g. execution time.
rowanmiller commentedon 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 😄
Ponant commentedon Dec 12, 2016
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
Standardize and consolidate logging
Standardize and consolidate logging
Standardize and consolidate logging
[-]Improve experiece for filtering log data[/-][+]Infrastructure: Improve experiece for filtering log data[/+]demodav commentedon Aug 30, 2018
How does one use this code? How do you execute it please provide an example.
ajcvickers commentedon Aug 30, 2018
@demodav This is old code used to describe an issue--it is not intended to run on recent releases.