本文整理汇总了C#中System.Web.Http.Tracing.TestTraceWriter类的典型用法代码示例。如果您正苦于以下问题:C# TestTraceWriter类的具体用法?C# TestTraceWriter怎么用?C# TestTraceWriter使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
TestTraceWriter类属于System.Web.Http.Tracing命名空间,在下文中一共展示了TestTraceWriter类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Info_With_Message_Traces
public void Info_With_Message_Traces()
{
// Arrange
TestTraceWriter traceWriter = new TestTraceWriter();
HttpRequestMessage request = new HttpRequestMessage();
TraceRecord[] expectedTraces = new TraceRecord[]
{
new TraceRecord(request, "testCategory", TraceLevel.Info) { Kind = TraceKind.Trace, Message = "The formatted message" },
};
// Act
traceWriter.Info(request, "testCategory", "The {0} message", "formatted");
// Assert
Assert.Equal<TraceRecord>(expectedTraces, traceWriter.Traces, new TraceRecordComparer());
}
示例2: Debug_With_Message_And_Exception_Traces
public void Debug_With_Message_And_Exception_Traces()
{
// Arrange
TestTraceWriter traceWriter = new TestTraceWriter();
HttpRequestMessage request = new HttpRequestMessage();
InvalidOperationException exception = new InvalidOperationException();
TraceRecord[] expectedTraces = new TraceRecord[]
{
new TraceRecord(request, "testCategory", TraceLevel.Debug) { Kind = TraceKind.Trace, Message = "The formatted message", Exception = exception },
};
// Act
traceWriter.Debug(request, "testCategory", exception, "The {0} message", "formatted");
// Assert
Assert.Equal<TraceRecord>(expectedTraces, traceWriter.Traces, new TraceRecordComparer());
}
示例3: TraceBeginEnd_Invokes_ErrorTrace_When_Tracing_Only_Errors
public void TraceBeginEnd_Invokes_ErrorTrace_When_Tracing_Only_Errors()
{
// Arrange
TestTraceWriter traceWriter = new TestTraceWriter();
traceWriter.TraceSelector = (rqst, category, level) => level >= TraceLevel.Error;
HttpRequestMessage request = new HttpRequestMessage();
Exception exception = new InvalidOperationException();
bool invoked = false;
// Act
Exception thrown = Assert.Throws<InvalidOperationException>(
() => traceWriter.TraceBeginEnd(request,
"",
TraceLevel.Off,
"",
"",
beginTrace: (tr) => { },
execute: () => { throw exception; },
endTrace: (tr) => { },
errorTrace: (tr) => { invoked = true; }));
// Assert
Assert.True(invoked);
Assert.Same(exception, thrown);
}
示例4: TraceBeginEnd_Does_Not_Invoke_ErrorTrace_Unless_Error_Occurs
public void TraceBeginEnd_Does_Not_Invoke_ErrorTrace_Unless_Error_Occurs()
{
// Arrange
TestTraceWriter traceWriter = new TestTraceWriter();
HttpRequestMessage request = new HttpRequestMessage();
bool invoked = false;
// Act
traceWriter.TraceBeginEnd(request,
"",
TraceLevel.Info,
"",
"",
beginTrace: (tr) => { },
execute: () => { },
endTrace: (tr) => { },
errorTrace: (tr) => { invoked = true; });
// Assert
Assert.False(invoked);
}
示例5: TraceBeginEndAsync_Traces
public void TraceBeginEndAsync_Traces()
{
// Arrange
TestTraceWriter traceWriter = new TestTraceWriter();
HttpRequestMessage request = new HttpRequestMessage();
TraceRecord[] expectedTraces = new TraceRecord[]
{
new TraceRecord(request, "testCategory", TraceLevel.Info) { Kind = TraceKind.Begin, Operator = "tester", Operation = "testOp", Message = "beginMessage" },
new TraceRecord(request, "testCategory", TraceLevel.Info) { Kind = TraceKind.End, Operator = "tester", Operation = "testOp", Message = "endMessage" },
};
// Act
traceWriter.TraceBeginEndAsync(request,
"testCategory",
TraceLevel.Info,
"tester",
"testOp",
beginTrace: (tr) => { tr.Message = "beginMessage"; },
execute: () => TaskHelpers.Completed(),
endTrace: (tr) => { tr.Message = "endMessage"; },
errorTrace: (tr) => { tr.Message = "won't happen"; }).Wait();
// Assert
Assert.Equal<TraceRecord>(expectedTraces, traceWriter.Traces, new TraceRecordComparer());
}
示例6: 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);
}
示例7: 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);
}
示例8: TraceBeginEndAsync_Invokes_ErrorTrace_When_Tracing_Only_Errors
public void TraceBeginEndAsync_Invokes_ErrorTrace_When_Tracing_Only_Errors()
{
// Arrange
TestTraceWriter traceWriter = new TestTraceWriter();
traceWriter.TraceSelector = (rqst, category, level) => level >= TraceLevel.Error;
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.Info,
"",
"",
beginTrace: (tr) => { },
execute: () => tcs.Task,
endTrace: (tr) => { },
errorTrace: (tr) => { invoked = true; }).Wait());
// Assert
Assert.True(invoked);
Assert.Same(exception, thrown);
}
示例9: 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");
}
示例10: TraceBeginEnd_Traces_And_Throws_AggregateException
public void TraceBeginEnd_Traces_And_Throws_AggregateException()
{
// 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.TraceBeginEnd(request,
"testCategory",
TraceLevel.Error,
"tester",
"testOp",
beginTrace: (tr) => { tr.Message = "beginMessage"; },
execute: () =>
{
AggregateException aggEx = CreateAggregateException(request);
// To satisfy TraceRecordComparer().
expectedTraces[1].Exception = aggEx;
throw aggEx;
},
endTrace: (tr) => { tr.Message = "won't Happen"; },
errorTrace: null));
// Assert
Assert.Equal<TraceRecord>(expectedTraces, traceWriter.Traces, new TraceRecordComparer());
Assert.Same(thrown, expectedTraces[1].Exception);
}
示例11: 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);
}
示例12: 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);
}
示例13: 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);
}
示例14: 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);
}
示例15: 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);
}