本文整理汇总了C#中System.Web.Http.Tracing.TestTraceWriter.TraceBeginEndAsync方法的典型用法代码示例。如果您正苦于以下问题:C# TestTraceWriter.TraceBeginEndAsync方法的具体用法?C# TestTraceWriter.TraceBeginEndAsync怎么用?C# TestTraceWriter.TraceBeginEndAsync使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Web.Http.Tracing.TestTraceWriter
的用法示例。
在下文中一共展示了TestTraceWriter.TraceBeginEndAsync方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: TraceBeginEndAsync_Invokes_EndTrace
public void TraceBeginEndAsync_Invokes_EndTrace()
{
// Arrange
TestTraceWriter traceWriter = new TestTraceWriter();
HttpRequestMessage request = new HttpRequestMessage();
bool invoked = false;
// Act
traceWriter.TraceBeginEndAsync(request,
"",
TraceLevel.Off,
"",
"",
beginTrace: (tr) => { },
execute: () => TaskHelpers.Completed(),
endTrace: (tr) => { invoked = true; },
errorTrace: (tr) => { }).Wait();
// Assert
Assert.True(invoked);
}
示例2: TraceBeginEndAsync_Does_Not_Invoke_End_Trace_When_Error_Occurs
public void TraceBeginEndAsync_Does_Not_Invoke_End_Trace_When_Error_Occurs()
{
// Arrange
TestTraceWriter traceWriter = new TestTraceWriter();
HttpRequestMessage request = new HttpRequestMessage();
bool invoked = false;
Exception exception = new InvalidOperationException();
TaskCompletionSource<object> tcs = new TaskCompletionSource<object>(null);
tcs.TrySetException(exception);
// Act
Exception thrown = Assert.Throws<InvalidOperationException>(
() => traceWriter.TraceBeginEndAsync(request,
"",
TraceLevel.Off,
"",
"",
beginTrace: (tr) => { },
execute: () => tcs.Task,
endTrace: (tr) => { invoked = true; },
errorTrace: (tr) => { }).Wait());
// Assert
Assert.False(invoked);
Assert.Same(exception, thrown);
}
示例3: TraceBeginEndAsync_Throws_With_Null_Execute_Action
public void TraceBeginEndAsync_Throws_With_Null_Execute_Action()
{
// Arrange
TestTraceWriter traceWriter = new TestTraceWriter();
HttpRequestMessage request = new HttpRequestMessage();
// Act & Assert
Assert.ThrowsArgumentNull(() => traceWriter.TraceBeginEndAsync(request,
"",
TraceLevel.Off,
"",
"",
beginTrace: null,
execute: null,
endTrace: null,
errorTrace: null),
"execute");
}
示例4: TraceBeginEndAsync_Accepts_Null_Trace_Actions
public void TraceBeginEndAsync_Accepts_Null_Trace_Actions()
{
// Arrange
TestTraceWriter traceWriter = new TestTraceWriter();
HttpRequestMessage request = new HttpRequestMessage();
// Act & Assert
Task t = traceWriter.TraceBeginEndAsync(request,
"",
TraceLevel.Off,
"",
"",
beginTrace: null,
execute: () => TaskHelpers.Completed(),
endTrace: null,
errorTrace: null);
t.Wait();
}
示例5: TraceBeginEndAsyncGeneric_Does_Not_Invoke_ErrorTrace_When_Tracing_Only_Higher_Level
public void TraceBeginEndAsyncGeneric_Does_Not_Invoke_ErrorTrace_When_Tracing_Only_Higher_Level()
{
// Arrange
TestTraceWriter traceWriter = new TestTraceWriter();
traceWriter.TraceSelector = (rqst, category, level) => level >= TraceLevel.Fatal;
HttpRequestMessage request = new HttpRequestMessage();
bool invoked = false;
Exception exception = new InvalidOperationException();
TaskCompletionSource<int> tcs = new TaskCompletionSource<int>(0);
tcs.TrySetException(exception);
// Act & Assert
Exception thrown = Assert.Throws<InvalidOperationException>(
() => traceWriter.TraceBeginEndAsync<int>(request,
"",
TraceLevel.Info,
"",
"",
beginTrace: (tr) => { },
execute: () => tcs.Task,
endTrace: (tr, value) => { },
errorTrace: (tr) => { invoked = true; }).Wait());
Assert.False(invoked);
Assert.Same(exception, thrown);
}
示例6: TraceBeginAsyncGeneric_Traces_And_Faults_When_Inner_Faults
public void TraceBeginAsyncGeneric_Traces_And_Faults_When_Inner_Faults()
{
// Arrange
TestTraceWriter traceWriter = new TestTraceWriter();
HttpRequestMessage request = new HttpRequestMessage();
InvalidOperationException exception = new InvalidOperationException();
TraceRecord[] expectedTraces = new TraceRecord[]
{
new TraceRecord(request, "testCategory", TraceLevel.Info) { Kind = TraceKind.Begin, Operator = "tester", Operation = "testOp", Message = "beginMessage" },
new TraceRecord(request, "testCategory", TraceLevel.Error) { Kind = TraceKind.End, Operator = "tester", Operation = "testOp", Message = "errorMessage", Exception = exception },
};
TaskCompletionSource<int> tcs = new TaskCompletionSource<int>(1);
tcs.TrySetException(exception);
// Act & Assert
InvalidOperationException thrown = Assert.Throws<InvalidOperationException>(
() => traceWriter.TraceBeginEndAsync<int>(request,
"testCategory",
TraceLevel.Info,
"tester",
"testOp",
beginTrace: (tr) => { tr.Message = "beginMessage"; },
execute: () => tcs.Task,
endTrace: (tr, value) => { tr.Message = "won't happen"; },
errorTrace: (tr) => { tr.Message = "errorMessage"; }).Wait());
// Assert
Assert.Equal<TraceRecord>(expectedTraces, traceWriter.Traces, new TraceRecordComparer());
Assert.Same(exception, thrown);
}
示例7: TraceBeginEndAsyncGeneric_Invokes_EndTrace
public void TraceBeginEndAsyncGeneric_Invokes_EndTrace()
{
// Arrange
TestTraceWriter traceWriter = new TestTraceWriter();
HttpRequestMessage request = new HttpRequestMessage();
bool invoked = false;
int invokedValue = 0;
// Act
traceWriter.TraceBeginEndAsync<int>(request,
"",
TraceLevel.Off,
"",
"",
beginTrace: (tr) => { },
execute: () => TaskHelpers.FromResult<int>(1),
endTrace: (tr, value) => { invoked = true; invokedValue = value; },
errorTrace: (tr) => { }).Wait();
// Assert
Assert.True(invoked);
Assert.Equal(1, invokedValue);
}
示例8: TraceBeginEndAsync_Does_Not_Invoke_ErrorTrace_Unless_Error_Occurs
public void TraceBeginEndAsync_Does_Not_Invoke_ErrorTrace_Unless_Error_Occurs()
{
// Arrange
TestTraceWriter traceWriter = new TestTraceWriter();
HttpRequestMessage request = new HttpRequestMessage();
bool invoked = false;
TaskCompletionSource<object> tcs = new TaskCompletionSource<object>(null);
tcs.TrySetResult(null);
// Act
traceWriter.TraceBeginEndAsync(
request,
"",
TraceLevel.Off,
"",
"",
beginTrace: (tr) => { },
execute: () => tcs.Task,
endTrace: (tr) => { },
errorTrace: (tr) => { invoked = true; }).Wait();
// Assert
Assert.False(invoked);
}
示例9: TraceBeginEndAsync_Does_Not_Trace_HttpResponseException_When_Tracing_Only_Higher_Level
public void TraceBeginEndAsync_Does_Not_Trace_HttpResponseException_When_Tracing_Only_Higher_Level()
{
// Arrange
TestTraceWriter traceWriter = new TestTraceWriter();
traceWriter.TraceSelector = (rqst, category, level) => level >= TraceLevel.Error;
HttpRequestMessage request = new HttpRequestMessage();
bool invoked = false;
Exception exception = new HttpResponseException(Net.HttpStatusCode.NotFound);
// Act
Exception thrown = Assert.Throws<HttpResponseException>(
() => traceWriter.TraceBeginEndAsync(request,
"",
TraceLevel.Info,
"",
"",
beginTrace: (tr) => { invoked = true; },
execute: () => TaskHelpers.FromError(exception),
endTrace: (tr) => { },
errorTrace: (tr) => { }).Wait());
// Assert
Assert.False(invoked);
Assert.Empty(traceWriter.Traces);
Assert.Same(thrown, exception);
}
示例10: TraceBeginEndAsyncGeneric_Invokes_Execute
public void TraceBeginEndAsyncGeneric_Invokes_Execute()
{
// Arrange
TestTraceWriter traceWriter = new TestTraceWriter();
HttpRequestMessage request = new HttpRequestMessage();
bool invoked = false;
// Act
int result = traceWriter.TraceBeginEndAsync<int>(request,
"",
TraceLevel.Fatal,
"",
"",
beginTrace: (tr) => { },
execute: () =>
{
invoked = true;
return TaskHelpers.FromResult<int>(1);
},
endTrace: (tr, value) => { },
errorTrace: (tr) => { }).Result;
// Assert
Assert.True(invoked);
Assert.Equal(1, result);
}
示例11: TraceBeginEndAsync_Traces_And_Throws_HttpResponseException
public void TraceBeginEndAsync_Traces_And_Throws_HttpResponseException()
{
// Arrange
TestTraceWriter traceWriter = new TestTraceWriter();
HttpRequestMessage request = new HttpRequestMessage();
HttpResponseException exception = new HttpResponseException(Net.HttpStatusCode.InternalServerError);
List<TraceRecord> expectedTraces = new List<TraceRecord>
{
new TraceRecord(request, "testCategory", TraceLevel.Info) { Kind = TraceKind.Begin, Operator = "tester", Operation = "testOp", Message = "beginMessage" },
new TraceRecord(request, "testCategory", TraceLevel.Error) { Kind = TraceKind.End, Operator = "tester", Operation = "testOp", Exception = exception, Message = "errorMessage",
Status = Net.HttpStatusCode.InternalServerError },
};
// Act
Exception thrown = Assert.Throws<HttpResponseException>(
() => traceWriter.TraceBeginEndAsync(request,
"testCategory",
TraceLevel.Info,
"tester",
"testOp",
beginTrace: (tr) => { tr.Message = "beginMessage"; },
execute: () => { throw exception; },
endTrace: (tr) => { tr.Message = "won't Happen"; },
errorTrace: (tr) => { tr.Message = "errorMessage"; }).Wait());
// Assert
Assert.Equal<TraceRecord>(expectedTraces, traceWriter.Traces, new TraceRecordComparer());
Assert.Same(thrown, exception);
}
示例12: TraceBeginEndAsyncGeneric_Traces_And_Throws_AggregateException
public void TraceBeginEndAsyncGeneric_Traces_And_Throws_AggregateException(bool isExThrownAtExecution)
{
// Arrange
TestTraceWriter traceWriter = new TestTraceWriter();
HttpRequestMessage request = new HttpRequestMessage();
List<TraceRecord> expectedTraces = new List<TraceRecord>
{
new TraceRecord(request, "testCategory", TraceLevel.Error) { Kind = TraceKind.Begin, Operator = "tester", Operation = "testOp", Message = "beginMessage" },
new TraceRecord(request, "testCategory", TraceLevel.Warn)
{
Kind = TraceKind.End, Operator = "tester", Operation = "testOp", Exception = null,
// In the aggregateException, only the httpResponseException with the highest status code will be reflected in the trace record's message.
// In this test case, it should be NotFound.
Message = "UserMessage='The request is invalid.', ModelStateError=[key=[error], username=[invalid]]",
Status = Net.HttpStatusCode.NotFound
},
};
// Act
// Errors and exceptions are instantiated in the `execute` func to avoid the variation of error messages for culture issues.
Exception thrown = Assert.Throws<AggregateException>(
() => traceWriter.TraceBeginEndAsync<int>(request,
"testCategory",
TraceLevel.Error,
"tester",
"testOp",
beginTrace: (tr) => { tr.Message = "beginMessage"; },
execute: () =>
{
AggregateException aggEx = CreateAggregateException(request);
Task<int> task = new Task<int>(() => { throw aggEx; });
// To satisfy TraceRecordComparer().
expectedTraces[1].Exception = aggEx;
if (isExThrownAtExecution)
{
throw aggEx;
}
task.Start();
return task;
},
endTrace: (tr, result) => { tr.Message = "won't Happen"; },
errorTrace: null).Wait());
// Assert
Assert.Equal<TraceRecord>(expectedTraces, traceWriter.Traces, new TraceRecordComparer());
Assert.Same(thrown, expectedTraces[1].Exception);
}
示例13: TraceBeginEndAsyncGeneric_Accepts_Null_Trace_Actions
public void TraceBeginEndAsyncGeneric_Accepts_Null_Trace_Actions()
{
// Arrange
TestTraceWriter traceWriter = new TestTraceWriter();
HttpRequestMessage request = new HttpRequestMessage();
// Act & Assert
Task t = traceWriter.TraceBeginEndAsync<int>(request,
"",
TraceLevel.Off,
"",
"",
beginTrace: null,
execute: () => Task.FromResult<int>(1),
endTrace: null,
errorTrace: null);
t.Wait();
}
示例14: TraceBeginEndAsync_Does_Not_Invoke_EndTrace_When_Tracing_Only_Higher_Level
public void TraceBeginEndAsync_Does_Not_Invoke_EndTrace_When_Tracing_Only_Higher_Level()
{
// Arrange
TestTraceWriter traceWriter = new TestTraceWriter();
traceWriter.TraceSelector = (rqst, category, level) => level >= TraceLevel.Error;
HttpRequestMessage request = new HttpRequestMessage();
bool invoked = false;
// Act
traceWriter.TraceBeginEndAsync(request,
"",
TraceLevel.Info,
"",
"",
beginTrace: (tr) => { },
execute: () => TaskHelpers.Completed(),
endTrace: (tr) => { invoked = true; },
errorTrace: (tr) => { }).Wait();
// Assert
Assert.False(invoked);
}
示例15: TraceBeginEndAsyncGeneric_Invokes_ErrorTrace
public void TraceBeginEndAsyncGeneric_Invokes_ErrorTrace()
{
// Arrange
TestTraceWriter traceWriter = new TestTraceWriter();
HttpRequestMessage request = new HttpRequestMessage();
bool invoked = false;
Exception exception = new InvalidOperationException();
TaskCompletionSource<int> tcs = new TaskCompletionSource<int>(0);
tcs.TrySetException(exception);
// Act
Exception thrown = Assert.Throws<InvalidOperationException>(
() => traceWriter.TraceBeginEndAsync<int>(request,
"",
TraceLevel.Off,
"",
"",
beginTrace: (tr) => { },
execute: () => tcs.Task,
endTrace: (tr, value) => { },
errorTrace: (tr) => { invoked = true; }).Wait());
// Assert
Assert.True(invoked);
Assert.Same(exception, thrown);
}