Log out SQL trace on error

Closes #394
pull/395/head
ta264 4 years ago
parent 9fc0a8d4d1
commit f58cffef51

@ -12,7 +12,7 @@ namespace NzbDrone.Common.Instrumentation
{
public static class NzbDroneLogger
{
private const string FILE_LOG_LAYOUT = @"${date:format=yy-M-d HH\:mm\:ss.f}|${level}|${logger}|${message}${onexception:inner=${newline}${newline}[v${assembly-version}] ${exception:format=ToString}${newline}}";
private const string FILE_LOG_LAYOUT = @"${date:format=yy-M-d HH\:mm\:ss.f}|${level}|${logger}|${message}${onexception:inner=${newline}${newline}[v${assembly-version}] ${exception:format=ToString}${newline}${exception:format=Data}${newline}}";
private static bool _isConfigured;
@ -94,7 +94,7 @@ namespace NzbDrone.Common.Instrumentation
{
DebuggerTarget target = new DebuggerTarget();
target.Name = "debuggerLogger";
target.Layout = "[${level}] [${threadid}] ${logger}: ${message} ${onexception:inner=${newline}${newline}[v${assembly-version}] ${exception:format=ToString}${newline}}";
target.Layout = "[${level}] [${threadid}] ${logger}: ${message} ${onexception:inner=${newline}${newline}[v${assembly-version}] ${exception:format=ToString}${newline}${exception:format=Data}${newline}}";
var loggingRule = new LoggingRule("*", LogLevel.Trace, target);
LogManager.Configuration.AddTarget("debugger", target);
@ -108,7 +108,7 @@ namespace NzbDrone.Common.Instrumentation
var coloredConsoleTarget = new ColoredConsoleTarget();
coloredConsoleTarget.Name = "consoleLogger";
coloredConsoleTarget.Layout = "[${level}] ${logger}: ${message} ${onexception:inner=${newline}${newline}[v${assembly-version}] ${exception:format=ToString}${newline}}";
coloredConsoleTarget.Layout = "[${level}] ${logger}: ${message} ${onexception:inner=${newline}${newline}[v${assembly-version}] ${exception:format=ToString}${newline}${exception:format=Data}${newline}}";
var loggingRule = new LoggingRule("*", level, coloredConsoleTarget);

@ -98,7 +98,7 @@ namespace NzbDrone.Core.Datastore
{
if (LogSql)
{
LogQuery(template.RawSql, (DynamicParameters)template.Parameters);
Logger.Trace(GetSqlLogString(template.RawSql, template.Parameters));
}
return template;
@ -108,12 +108,14 @@ namespace NzbDrone.Core.Datastore
{
if (LogSql)
{
LogQuery(sql, new DynamicParameters(parameters));
Logger.Trace(GetSqlLogString(sql, parameters));
}
}
private static void LogQuery(string sql, DynamicParameters parameters)
public static string GetSqlLogString(string sql, object paramsObject)
{
var parameters = new DynamicParameters(paramsObject);
var sb = new StringBuilder();
sb.AppendLine();
sb.AppendLine("==== Begin Query Trace ====");
@ -122,6 +124,7 @@ namespace NzbDrone.Core.Datastore
sb.AppendLine(sql);
sb.AppendLine();
sb.AppendLine("PARAMETERS:");
foreach (var p in parameters.ToDictionary())
{
var val = (p.Value is string) ? string.Format("\"{0}\"", p.Value) : p.Value;
@ -132,7 +135,7 @@ namespace NzbDrone.Core.Datastore
sb.AppendLine("==== End Query Trace ====");
sb.AppendLine();
Logger.Trace(sb.ToString());
return sb.ToString();
}
private static Dictionary<string, object> ToDictionary(this DynamicParameters dynamicParams)

@ -11,7 +11,17 @@ namespace NzbDrone.Core.Datastore
{
using (var conn = db.OpenConnection())
{
var items = SqlMapper.Query<T>(conn, sql, param);
IEnumerable<T> items;
try
{
items = SqlMapper.Query<T>(conn, sql, param);
}
catch (Exception e)
{
e.Data.Add("SQL", SqlBuilderExtensions.GetSqlLogString(sql, param));
throw;
}
if (TableMapping.Mapper.LazyLoadList.TryGetValue(typeof(T), out var lazyProperties))
{
foreach (var item in items)
@ -26,25 +36,30 @@ namespace NzbDrone.Core.Datastore
public static IEnumerable<TReturn> Query<TFirst, TSecond, TReturn>(this IDatabase db, string sql, Func<TFirst, TSecond, TReturn> map, object param = null, IDbTransaction transaction = null, bool buffered = true, string splitOn = "Id", int? commandTimeout = null, CommandType? commandType = null)
{
TReturn mapWithLazy(TFirst first, TSecond second)
TReturn MapWithLazy(TFirst first, TSecond second)
{
ApplyLazyLoad(db, first);
ApplyLazyLoad(db, second);
return map(first, second);
}
IEnumerable<TReturn> result = null;
using (var conn = db.OpenConnection())
{
result = SqlMapper.Query<TFirst, TSecond, TReturn>(conn, sql, mapWithLazy, param, transaction, buffered, splitOn, commandTimeout, commandType);
try
{
return SqlMapper.Query<TFirst, TSecond, TReturn>(conn, sql, MapWithLazy, param, transaction, buffered, splitOn, commandTimeout, commandType);
}
catch (Exception e)
{
e.Data.Add("SQL", SqlBuilderExtensions.GetSqlLogString(sql, param));
throw;
}
}
return result;
}
public static IEnumerable<TReturn> Query<TFirst, TSecond, TThird, TReturn>(this IDatabase db, string sql, Func<TFirst, TSecond, TThird, TReturn> map, object param = null, IDbTransaction transaction = null, bool buffered = true, string splitOn = "Id", int? commandTimeout = null, CommandType? commandType = null)
{
TReturn mapWithLazy(TFirst first, TSecond second, TThird third)
TReturn MapWithLazy(TFirst first, TSecond second, TThird third)
{
ApplyLazyLoad(db, first);
ApplyLazyLoad(db, second);
@ -52,18 +67,23 @@ namespace NzbDrone.Core.Datastore
return map(first, second, third);
}
IEnumerable<TReturn> result = null;
using (var conn = db.OpenConnection())
{
result = SqlMapper.Query<TFirst, TSecond, TThird, TReturn>(conn, sql, mapWithLazy, param, transaction, buffered, splitOn, commandTimeout, commandType);
try
{
return SqlMapper.Query<TFirst, TSecond, TThird, TReturn>(conn, sql, MapWithLazy, param, transaction, buffered, splitOn, commandTimeout, commandType);
}
catch (Exception e)
{
e.Data.Add("SQL", SqlBuilderExtensions.GetSqlLogString(sql, param));
throw;
}
}
return result;
}
public static IEnumerable<TReturn> Query<TFirst, TSecond, TThird, TFourth, TReturn>(this IDatabase db, string sql, Func<TFirst, TSecond, TThird, TFourth, TReturn> map, object param = null, IDbTransaction transaction = null, bool buffered = true, string splitOn = "Id", int? commandTimeout = null, CommandType? commandType = null)
{
TReturn mapWithLazy(TFirst first, TSecond second, TThird third, TFourth fourth)
TReturn MapWithLazy(TFirst first, TSecond second, TThird third, TFourth fourth)
{
ApplyLazyLoad(db, first);
ApplyLazyLoad(db, second);
@ -72,18 +92,23 @@ namespace NzbDrone.Core.Datastore
return map(first, second, third, fourth);
}
IEnumerable<TReturn> result = null;
using (var conn = db.OpenConnection())
{
result = SqlMapper.Query<TFirst, TSecond, TThird, TFourth, TReturn>(conn, sql, mapWithLazy, param, transaction, buffered, splitOn, commandTimeout, commandType);
try
{
return SqlMapper.Query<TFirst, TSecond, TThird, TFourth, TReturn>(conn, sql, MapWithLazy, param, transaction, buffered, splitOn, commandTimeout, commandType);
}
catch (Exception e)
{
e.Data.Add("SQL", SqlBuilderExtensions.GetSqlLogString(sql, param));
throw;
}
}
return result;
}
public static IEnumerable<TReturn> Query<TFirst, TSecond, TThird, TFourth, TFifth, TReturn>(this IDatabase db, string sql, Func<TFirst, TSecond, TThird, TFourth, TFifth, TReturn> map, object param = null, IDbTransaction transaction = null, bool buffered = true, string splitOn = "Id", int? commandTimeout = null, CommandType? commandType = null)
{
TReturn mapWithLazy(TFirst first, TSecond second, TThird third, TFourth fourth, TFifth fifth)
TReturn MapWithLazy(TFirst first, TSecond second, TThird third, TFourth fourth, TFifth fifth)
{
ApplyLazyLoad(db, first);
ApplyLazyLoad(db, second);
@ -93,13 +118,18 @@ namespace NzbDrone.Core.Datastore
return map(first, second, third, fourth, fifth);
}
IEnumerable<TReturn> result = null;
using (var conn = db.OpenConnection())
{
result = SqlMapper.Query<TFirst, TSecond, TThird, TFourth, TFifth, TReturn>(conn, sql, mapWithLazy, param, transaction, buffered, splitOn, commandTimeout, commandType);
try
{
return SqlMapper.Query<TFirst, TSecond, TThird, TFourth, TFifth, TReturn>(conn, sql, MapWithLazy, param, transaction, buffered, splitOn, commandTimeout, commandType);
}
catch (Exception e)
{
e.Data.Add("SQL", SqlBuilderExtensions.GetSqlLogString(sql, param));
throw;
}
}
return result;
}
public static IEnumerable<T> Query<T>(this IDatabase db, SqlBuilder builder)

Loading…
Cancel
Save