forked from ThatRendle/Simple.Data
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTraceHelper.cs
More file actions
41 lines (39 loc) · 1.47 KB
/
TraceHelper.cs
File metadata and controls
41 lines (39 loc) · 1.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
using System;
using System.Data;
using System.Data.Common;
using System.Diagnostics;
using System.Linq;
using System.Text;
namespace Simple.Data.Ado
{
public static class TraceHelper
{
private static readonly string EOL = Environment.NewLine;
public static void WriteTrace (this IDbCommand command)
{
if (!SimpleDataTraceSources.TraceSource.Switch.Level.HasFlag(SourceLevels.Information))
return;
try
{
var sb = new StringBuilder();
sb.AppendFormat("SQL command (CommandType={0}):{1} {2}", command.CommandType, EOL, command.CommandText);
if (command.Parameters.Count > 0)
{
sb.AppendFormat("{0}Parameters:", EOL);
foreach (var parameter in command.Parameters.OfType<DbParameter>())
{
object strValue = parameter.Value is string ? string.Format("\"{0}\"", parameter.Value) : parameter.Value;
sb.AppendFormat("{0} {1} ({2}) = {3}", EOL, parameter.ParameterName, parameter.DbType, strValue);
}
}
SimpleDataTraceSources.TraceSource.TraceEvent(TraceEventType.Information, SimpleDataTraceSources.SqlMessageId, sb.ToString());
}
catch (Exception)
{
#if(DEBUG)
throw;
#endif
}
}
}
}