How can i catch SqlConnection.InfoMessage
and save it into string
? I couldn't find any examples so i'm asking here.
Sample operation on database:
public async Task AddAsync(Items record)
{
using (var context = new DBEntities())
{
context.Configuration.LazyLoadingEnabled = false;
context.Items.Add(record);
await context.SaveChangesAsync();
}
}
How can i do this on this example?
If i understand correctly it will let me catch PRINT
messages and this is exactly what i want to do.
Edit
Am i doing this correctly?
public async Task AddAsync(Items record)
{
using (var context = new DBEntities())
{
var con = (SqlConnection)context.Database.Connection;
context.Configuration.LazyLoadingEnabled = false;
context.Items.Add(record);
await context.SaveChangesAsync();
con.InfoMessage += (obj, args) =>
{
SQLServerLogHandler.LogDisplay.LogPrint = args.Message;
};
}
}
I'm interested only in latest message from the server.SQLServerLogHandler.LogDisplay.LogPrint
is just static string.
EF6:
var messages = new List<string>();
var con = (SqlConnection)db.Database.Connection;
con.InfoMessage += (s, a) =>
{
messages.Add(a.Message);
};
db.Database.ExecuteSqlCommand("print 'foo'");
EF Core:
var messages = new List<string>();
var con = (SqlConnection)db.Database.GetDbConnection();
con.InfoMessage += (s, a) =>
{
messages.Add(a.Message);
};
db.Database.ExecuteSqlRaw("print 'foo'");