EntityFramework优化:SQL语句日志
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了EntityFramework优化:SQL语句日志,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4954字,纯文字阅读大概需要8分钟。
内容图文
System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using System.Data.Entity.Infrastructure.Interception;
using System.Diagnostics;
using System.Data.Common;
using NLog;
namespace Libing.Portal.Web.Common.Interceptors
{
public class NLogDbCommandInterceptor : DbCommandInterceptor
{
private static readonly Stopwatch watch =
new Stopwatch();
private static readonly Logger logger =
LogManager.GetCurrentClassLogger();
public override void ScalarExecuting(DbCommand command, DbCommandInterceptionContext<
object>
interceptionContext)
{
base.ScalarExecuting(command, interceptionContext);
watch.Restart();
}
public override void ScalarExecuted(DbCommand command, DbCommandInterceptionContext<
object>
interceptionContext)
{
watch.Stop();
if (interceptionContext.Exception !=
null)
{
logger.Error("Exception:{1} \r\n --> Error executing command: {0}", command.CommandText, interceptionContext.Exception.ToString());
}
else
{
StringBuilder message =
new StringBuilder();
message.AppendFormat("\r\n-->{0}", command.CommandText);
foreach (DbParameter parameter
in command.Parameters)
{
message.AppendFormat("\r\n-- {0}: ‘{1}‘ (Type = {2}, IsNullable = {3})", parameter.ParameterName, parameter.Value, parameter.DbType, parameter.IsNullable);
}
message.AppendFormat("\r\n-- Completed in {0} ms", watch.ElapsedMilliseconds, command);
logger.Trace(message.ToString());
}
base.ScalarExecuted(command, interceptionContext);
}
public override void NonQueryExecuting(DbCommand command, DbCommandInterceptionContext<
int>
interceptionContext)
{
base.NonQueryExecuting(command, interceptionContext);
watch.Restart();
}
public override void NonQueryExecuted(DbCommand command, DbCommandInterceptionContext<
int>
interceptionContext)
{
watch.Stop();
if (interceptionContext.Exception !=
null)
{
logger.Error("Exception:{1} \r\n --> Error executing command: {0}", command.CommandText, interceptionContext.Exception.ToString());
}
else
{
StringBuilder message =
new StringBuilder();
message.AppendFormat("\r\n-->{0}", command.CommandText);
foreach (DbParameter parameter
in command.Parameters)
{
message.AppendFormat("\r\n-- {0}: ‘{1}‘ (Type = {2}, IsNullable = {3})", parameter.ParameterName, parameter.Value, parameter.DbType, parameter.IsNullable);
}
message.AppendFormat("\r\n-- Completed in {0} ms", watch.ElapsedMilliseconds, command);
logger.Trace(message.ToString());
}
base.NonQueryExecuted(command, interceptionContext);
}
public override void ReaderExecuting(DbCommand command, DbCommandInterceptionContext<DbDataReader>
interceptionContext)
{
base.ReaderExecuting(command, interceptionContext);
watch.Restart();
}
public override void ReaderExecuted(DbCommand command, DbCommandInterceptionContext<DbDataReader>
interceptionContext)
{
watch.Stop();
if (interceptionContext.Exception !=
null)
{
logger.Error("Exception:{1} \r\n --> Error executing command: {0}", command.CommandText, interceptionContext.Exception.ToString());
}
else
{
StringBuilder message =
new StringBuilder();
message.AppendFormat("\r\n-->{0}", command.CommandText);
foreach (DbParameter parameter
in command.Parameters)
{
message.AppendFormat("\r\n-- {0}: ‘{1}‘ (Type = {2}, IsNullable = {3})", parameter.ParameterName, parameter.Value, parameter.DbType, parameter.IsNullable);
}
message.AppendFormat("\r\n-- Completed in {0} ms", watch.ElapsedMilliseconds, command);
logger.Trace(message.ToString());
}
base.ReaderExecuted(command, interceptionContext);
}
}
}
NLogDbCommandInterceptor.cs
![EntityFramework优化:SQL语句日志 - 文章图片]()
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Data.Entity.Infrastructure.Interception;
using Libing.Portal.Web.Common.Interceptors;
namespace Libing.Portal.Web.Data
{
public class PortalContext : DbContext
{
static PortalContext()
{
Database.SetInitializer<PortalContext>(null);
// 日志:Entity Framework生成的Sql语句
DbInterception.Add(new NLogDbCommandInterceptor());
}
}
}
PortalContext.cs
![EntityFramework优化:SQL语句日志 - 文章图片]()
<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.nlog-project.org/schemas/NLog.xsd NLog.xsd"
autoReload="true">
<targets>
<target name="file" xsi:type="AsyncWrapper" queueLimit="5000" overflowAction="Discard">
<target xsi:type="File"
fileName="${basedir}/Logs/${shortdate}.log"
layout="${longdate} ${level:uppercase=true} ${event-context:item=Action} ${message} ${event-context:item=Amount} ${stacktrace}"
keepFileOpen="false"
archiveFileName="${basedir}/Logs/${shortdate}.{##}.log"
archiveAboveSize="1048576"
encoding="UTF-8" />
</target>
</targets>
<rules>
<!--Trace->Debug->Info->Warn->Error->Fatal-->
<logger name="*" minlevel="Trace" writeTo="file" />
</rules>
</nlog>
NLog.configEntityFramework优化:SQL语句日志
标签:ali common auto erro getc ace alc version archive
本文系统来源:https://www.cnblogs.com/libingql/p/11534385.html
内容总结
以上是互联网集市为您收集整理的EntityFramework优化:SQL语句日志全部内容,希望文章能够帮你解决EntityFramework优化:SQL语句日志所遇到的程序开发问题。
如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
来源:【匿名】