Permalink
Browse files

Update #283 - Add async support so we don't crash

  • Loading branch information...
1 parent c299132 commit 68e6ee97bcd30c79d38c5e1d8ac2435ea2eaf3ff @avanderhoorn avanderhoorn committed Mar 27, 2013
View
31 source/Glimpse.Ado/AlternateType/GlimpseDbCommand.cs
@@ -52,7 +52,7 @@ private IMessageBroker MessageBroker
private IExecutionTimer TimerStrategy
{
- get { return timerStrategy ?? (timerStrategy = GlimpseConfiguration.GetExecutionTimer()); }
+ get { return timerStrategy ?? (timerStrategy = GlimpseConfiguration.GetConfiguredTimerStrategy()()); }
set { timerStrategy = value; }
}
@@ -175,7 +175,7 @@ protected override DbDataReader ExecuteDbDataReader(CommandBehavior behavior)
DbDataReader reader;
var commandId = Guid.NewGuid();
- var timer = TimerTrigger();
+ var timer = LogCommandSeed();
LogCommandStart(commandId, timer);
try
{
@@ -197,7 +197,7 @@ public override int ExecuteNonQuery()
int num;
var commandId = Guid.NewGuid();
- var timer = TimerTrigger();
+ var timer = LogCommandSeed();
LogCommandStart(commandId, timer);
try
{
@@ -218,7 +218,7 @@ public override object ExecuteScalar()
object result;
var commandId = Guid.NewGuid();
- var timer = TimerTrigger();
+ var timer = LogCommandSeed();
LogCommandStart(commandId, timer);
try
{
@@ -286,6 +286,11 @@ private static object GetParameterValue(IDataParameter parameter)
return parameter.Value;
}
+ private TimeSpan LogCommandSeed()
+ {
+ return TimerStrategy != null ? TimerStrategy.Start() : TimeSpan.Zero;
+ }
+
private void LogCommandStart(Guid commandId, TimeSpan timerTimeSpan)
{
if (MessageBroker != null)
@@ -314,35 +319,25 @@ private void LogCommandStart(Guid commandId, TimeSpan timerTimeSpan)
private void LogCommandEnd(Guid commandId, TimeSpan timer, int? recordsAffected, string type)
{
- if (MessageBroker != null)
+ if (MessageBroker != null && TimerStrategy != null)
{
MessageBroker.Publish(
new CommandDurationAndRowCountMessage(InnerConnection.ConnectionId, commandId, recordsAffected)
- .AsTimedMessage(TimerStop(timer))
+ .AsTimedMessage(TimerStrategy.Stop(timer))
.AsTimelineMessage("Command Executed", AdoTimelineCategory.Command, type));
}
}
private void LogCommandError(Guid commandId, TimeSpan timer, Exception exception, string type)
{
- if (MessageBroker != null)
+ if (MessageBroker != null && TimerStrategy != null)
{
MessageBroker.Publish(
new CommandErrorMessage(InnerConnection.ConnectionId, commandId, exception)
- .AsTimedMessage(TimerStop(timer))
+ .AsTimedMessage(TimerStrategy.Stop(timer))
.AsTimelineMessage("Command Error", AdoTimelineCategory.Command, type));
}
}
-
- private TimeSpan TimerTrigger()
- {
- return TimerStrategy != null ? TimerStrategy.Start() : TimeSpan.Zero;
- }
-
- private TimerResult TimerStop(TimeSpan timer)
- {
- return TimerStrategy != null ? TimerStrategy.Stop(timer) : null;
- }
#endregion
}
}
View
22 source/Glimpse.Ado/AlternateType/GlimpseDbConnection.cs
@@ -33,7 +33,7 @@ public GlimpseDbConnection(DbConnection connection, DbProviderFactory providerFa
InnerProviderFactory = providerFactory;
ConnectionId = connectionId;
- if (MessageBroker != null)
+ if (MessageBroker != null && TimerStrategy != null)
{
if (connection.State == ConnectionState.Open)
{
@@ -65,7 +65,7 @@ private IMessageBroker MessageBroker
private IExecutionTimer TimerStrategy
{
- get { return timerStrategy ?? (timerStrategy = GlimpseConfiguration.GetExecutionTimer()); }
+ get { return timerStrategy ?? (timerStrategy = GlimpseConfiguration.GetConfiguredTimerStrategy()()); }
set { timerStrategy = value; }
}
@@ -138,6 +138,7 @@ public override void EnlistTransaction(Transaction transaction)
if (transaction != null)
{
transaction.TransactionCompleted += OnDtcTransactionCompleted;
+
if (MessageBroker != null)
{
MessageBroker.Publish(new DtcTransactionEnlistedMessage(ConnectionId, transaction.IsolationLevel));
@@ -182,6 +183,7 @@ protected override void Dispose(bool disposing)
InnerConnection.Dispose();
InnerConnection.StateChange -= StateChangeHaneler;
}
+
InnerConnection = null;
InnerProviderFactory = null;
base.Dispose(disposing);
@@ -224,7 +226,7 @@ private void OpenConnection()
ConnectionId = Guid.NewGuid();
}
- timerTimeSpan = TimerTrigger();
+ timerTimeSpan = TimerStrategy.Start();
MessageBroker.Publish(
new ConnectionStartedMessage(ConnectionId)
@@ -237,18 +239,8 @@ private void ClosedConnection()
MessageBroker.Publish(
new ConnectionClosedMessage(ConnectionId)
- .AsTimedMessage(TimerStop(timerTimeSpan))
+ .AsTimedMessage(TimerStrategy.Stop(timerTimeSpan))
.AsTimelineMessage("Connection Opened", AdoTimelineCategory.Connection));
- }
-
- private TimeSpan TimerTrigger()
- {
- return TimerStrategy != null ? TimerStrategy.Start() : TimeSpan.Zero;
- }
-
- private TimerResult TimerStop(TimeSpan timer)
- {
- return TimerStrategy != null ? TimerStrategy.Stop(timer) : null;
- }
+ }
}
}
View
37 source/Glimpse.Ado/AlternateType/GlimpseDbTransaction.cs
@@ -20,9 +20,9 @@ public GlimpseDbTransaction(DbTransaction transaction, GlimpseDbConnection conne
InnerConnection = connection;
TransactionId = Guid.NewGuid();
- if (MessageBroker != null)
+ if (MessageBroker != null && TimerStrategy != null)
{
- timerTimeSpan = TimerTrigger();
+ timerTimeSpan = TimerStrategy.Start();
MessageBroker.Publish(
new TransactionBeganMessage(connection.ConnectionId, TransactionId, transaction.IsolationLevel)
@@ -51,7 +51,7 @@ private IMessageBroker MessageBroker
private IExecutionTimer TimerStrategy
{
- get { return timerStrategy ?? (timerStrategy = GlimpseConfiguration.GetExecutionTimer()); }
+ get { return timerStrategy ?? (timerStrategy = GlimpseConfiguration.GetConfiguredTimerStrategy()()); }
set { timerStrategy = value; }
}
@@ -68,43 +68,34 @@ public override IsolationLevel IsolationLevel
public override void Commit()
{
InnerTransaction.Commit();
- if (MessageBroker != null)
+
+ if (MessageBroker != null && TimerStrategy != null)
{
MessageBroker.Publish(
new TransactionCommitMessage(InnerConnection.ConnectionId, TransactionId)
- .AsTimedMessage(TimerStop(timerTimeSpan)));
+ .AsTimedMessage(TimerStrategy.Stop(timerTimeSpan)));
}
}
- protected override void Dispose(bool disposing)
- {
- if (disposing)
- {
- InnerTransaction.Dispose();
- }
- base.Dispose(disposing);
- }
-
public override void Rollback()
{
InnerTransaction.Rollback();
- if (MessageBroker != null)
+ if (MessageBroker != null && TimerStrategy != null)
{
MessageBroker.Publish(
new TransactionRollbackMessage(InnerConnection.ConnectionId, TransactionId)
- .AsTimedMessage(TimerStop(timerTimeSpan)));
+ .AsTimedMessage(TimerStrategy.Stop(timerTimeSpan)));
}
}
- private TimeSpan TimerTrigger()
- {
- return TimerStrategy != null ? TimerStrategy.Start() : TimeSpan.Zero;
- }
-
- private TimerResult TimerStop(TimeSpan timer)
+ protected override void Dispose(bool disposing)
{
- return TimerStrategy != null ? TimerStrategy.Stop(timer) : null;
+ if (disposing)
+ {
+ InnerTransaction.Dispose();
+ }
+ base.Dispose(disposing);
}
}
}
View
22 source/Glimpse.Core/Framework/GlimpseConfiguration.cs
@@ -611,15 +611,19 @@ public static ILogger GetLogger()
[Obsolete("HACK: To support TraceListener with TraceSource via web.config")]
public static Func<IExecutionTimer> GetConfiguredTimerStrategy()
{
- return timerStrategy;
- }
-
- [Obsolete("HACK: To support TraceListener with TraceSource via web.config")]
- public static IExecutionTimer GetExecutionTimer()
- {
- var result = GetConfiguredTimerStrategy();
- return result != null ? result() : null;
- }
+ return () =>
+ {
+ try
+ {
+ return timerStrategy();
+ }
+ catch
+ {
+ // Avoid exception being thrown from threads without access to request store
+ return null;
+ }
+ };
+ }
[Obsolete("HACK: To support TraceListener with TraceSource via web.config")]
public static IMessageBroker GetConfiguredMessageBroker()
View
13 source/Glimpse.Core/TraceListener.cs
@@ -30,18 +30,7 @@ public TraceListener(string initializeData) : this(GlimpseConfiguration.GetConfi
public TraceListener(IMessageBroker messageBroker, Func<IExecutionTimer> timerStrategy)
{
MessageBroker = messageBroker;
- TimerStrategy = () =>
- {
- try
- {
- return timerStrategy();
- }
- catch
- {
- // Avoid exception being thrown from threads without access to request store
- return null;
- }
- };
+ TimerStrategy = timerStrategy;
}
internal IMessageBroker MessageBroker

0 comments on commit 68e6ee9

Please sign in to comment.