ef 输出sql 怎么一起吧对应的方法位置一起打印

2019-07-24 20:49发布

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 所在的位置一起打印

标签: ef
4条回答
欢心
2楼-- · 2019-07-24 21:33

不知道你日志组件用的啥?比如用 NLog,如下写法就可以:

ILogger logger = LogManager.GetCurrentClassLogger();
logger.Info("xxx");

Log4net 的方法也一样,用当前类的实例来记录日志即可。

查看更多
对你真心纯属浪费
3楼-- · 2019-07-24 21:36

public string GetCodeLineAndFileName()
{
StackTrace insStackTrace = new StackTrace(true);
StackFrame insStackFrame = insStackTrace.GetFrame(3);
return String.Format("File: {0}, Line: {1}", insStackFrame.GetFileName(), insStackFrame.GetFileLineNumber());
}

查看更多
The star\"
4楼-- · 2019-07-24 21:40

如果使用 ef core ,可以轻松搞定

查看更多
Lonely孤独者°
5楼-- · 2019-07-24 21:49

可以用调用方信息特性

CallerFilePathAttribute 包含调用方的源文件的完整路径。 这是编译时的文件路径。 String
CallerLineNumberAttribute 源文件中调用方法的行号。 Integer
CallerMemberNameAttribute 调用方的方法或属性名称。 请参阅本主题后面的成员名称。 String

https://www.cnblogs.com/SavionZhang/p/11192351.html

查看更多
登录 后发表回答