ef 输出sql 怎么一起吧对应的方法位置一起打印
比如
2019-07-24 09:05:56,888 [13] INFO BigZata.Models.BigZata_Entities [<.ctor>b__0] - Opened connection at 7/24/2019 9:05:56 AM +08:00
2019-07-24 09:05:57,123 [13] INFO BigZata.Models.BigZata_Entities [<.ctor>b__0] - SELECT
[Project1].[Account] AS [Account],
[Project1].[UserId] AS [UserId],
[Project1].[Page_Name] AS [Page_Name],
[Project1].[Preference_Type] AS [Preference_Type],
[Project1].[Column_Name] AS [Column_Name],
[Project1].[Group_ID] AS [Group_ID],
[Project1].[History] AS [History],
[Project1].[Is_Hidden] AS [Is_Hidden],
[Project1].[Page_Number] AS [Page_Number],
[Project1].[Create_Date] AS [Create_Date],
[Project1].[Create_User] AS [Create_User],
[Project1].[Update_Date] AS [Update_Date],
[Project1].[Update_User] AS [Update_User]
FROM ( SELECT
[Extent1].[Account] AS [Account],
[Extent1].[UserId] AS [UserId],
[Extent1].[Page_Name] AS [Page_Name],
[Extent1].[Preference_Type] AS [Preference_Type],
[Extent1].[Column_Name] AS [Column_Name],
[Extent1].[Group_ID] AS [Group_ID],
[Extent1].[History] AS [History],
[Extent1].[Is_Hidden] AS [Is_Hidden],
[Extent1].[Page_Number] AS [Page_Number],
[Extent1].[Create_Date] AS [Create_Date],
[Extent1].[Create_User] AS [Create_User],
[Extent1].[Update_Date] AS [Update_Date],
[Extent1].[Update_User] AS [Update_User]
FROM [dbo].[User_Grid_Preferences] AS [Extent1]
WHERE ([Extent1].[Account] = @p__linq__0) AND ([Extent1].[UserId] = @p__linq__1) AND ([Extent1].[Page_Name] = @p__linq__2)
) AS [Project1]
ORDER BY [Project1].[Preference_Type] ASC, [Project1].[Column_Name] ASC
2019-07-24 09:05:57,124 [13] INFO BigZata.Models.BigZata_Entities [<.ctor>b__0] -
2019-07-24 09:05:57,127 [13] INFO BigZata.Models.BigZata_Entities [<.ctor>b__0] - -- p__linq__0: 'S_Acct_90001555' (Type = AnsiString, Size = 8000)
2019-07-24 09:05:57,128 [13] INFO BigZata.Models.BigZata_Entities [<.ctor>b__0] - -- p__linq__1: 'U_User_00000001' (Type = AnsiString, Size = 8000)
2019-07-24 09:05:57,129 [13] INFO BigZata.Models.BigZata_Entities [<.ctor>b__0] - -- p__linq__2: '.ProductResearch.index' (Type = AnsiString, Size = 8000)
2019-07-24 09:05:57,130 [13] INFO BigZata.Models.BigZata_Entities [<.ctor>b__0] - -- Executing at 7/24/2019 9:05:57 AM +08:00
2019-07-24 09:05:57,356 [13] INFO BigZata.Models.BigZata_Entities [<.ctor>b__0] - -- Completed in 224 ms with result: SqlDataReader
2019-07-24 09:05:57,357 [13] INFO BigZata.Models.BigZata_Entities [<.ctor>b__0] -
2019-07-24 09:05:57,358 [13] INFO BigZata.Models.BigZata_Entities [<.ctor>b__0] - Closed connection at 7/24/2019 9:05:57 AM +08:00
怎么把linq 所在的位置一起打印
public string GetCodeLineAndFileName()
{
StackTrace insStackTrace = new StackTrace(true);
StackFrame insStackFrame = insStackTrace.GetFrame(3);
return String.Format("File: {0}, Line: {1}", insStackFrame.GetFileName(), insStackFrame.GetFileLineNumber());
}
如果使用 ef core ,可以轻松搞定
core 的 日志是 打出来了 但是 linq 语句的位置 怎么处理呢
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter)
{
//ef core执行数据库查询时的categoryName为Microsoft.EntityFrameworkCore.Database.Command,日志级别为Information
if (categoryName == "Microsoft.EntityFrameworkCore.Database.Command"
&& logLevel == LogLevel.Information)
{
var logContent = formatter(state, exception);
NLogger.Debug(logContent);
//TraceMessage("Something happened.");
// NLogger.Info(GetCodeLineAndFileName());
//TODO: 拿到日志内容想怎么玩就怎么玩吧
Console.WriteLine();
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine(logContent);
Console.ResetColor();
}
@conan_lin: 可以使用 ef core 的 TagWith ,不用自己写日志,只要修改一下 EF Core 的日志级别。
可以用调用方信息特性
CallerFilePathAttribute 包含调用方的源文件的完整路径。 这是编译时的文件路径。 String
CallerLineNumberAttribute 源文件中调用方法的行号。 Integer
CallerMemberNameAttribute 调用方的方法或属性名称。 请参阅本主题后面的成员名称。 String