本文整理汇总了C#中System.Web.Http.Tracing.TestTraceWriter.TraceBeginEnd方法的典型用法代码示例。如果您正苦于以下问题:C# TestTraceWriter.TraceBeginEnd方法的具体用法?C# TestTraceWriter.TraceBeginEnd怎么用?C# TestTraceWriter.TraceBeginEnd使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Web.Http.Tracing.TestTraceWriter
的用法示例。
在下文中一共展示了TestTraceWriter.TraceBeginEnd方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: TraceBeginEnd_Traces_And_Throws_When_Execute_Throws
public void TraceBeginEnd_Traces_And_Throws_When_Execute_Throws()
{
// Arrange
TestTraceWriter traceWriter = new TestTraceWriter();
HttpRequestMessage request = new HttpRequestMessage();
InvalidOperationException exception = new InvalidOperationException("test exception");
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", Exception = exception, Message = "errorMessage" },
};
// Act
Exception thrown = Assert.Throws<InvalidOperationException>(
() => traceWriter.TraceBeginEnd(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"; }));
// Assert
Assert.Equal<TraceRecord>(expectedTraces, traceWriter.Traces, new TraceRecordComparer());
Assert.Same(exception, thrown);
}
示例2: 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);
}
示例3: TraceBeginEnd_Traces
public void TraceBeginEnd_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.TraceBeginEnd(request,
"testCategory",
TraceLevel.Info,
"tester",
"testOp",
beginTrace: (tr) => { tr.Message = "beginMessage"; },
execute: () => { },
endTrace: (tr) => { tr.Message = "endMessage"; },
errorTrace: (tr) => { tr.Message = "won't happen"; });
// Assert
Assert.Equal<TraceRecord>(expectedTraces, traceWriter.Traces, new TraceRecordComparer());
}
示例4: TraceBeginEnd_Does_Not_Invoke_EndTrace_When_Tracing_Only_High_Level
public void TraceBeginEnd_Does_Not_Invoke_EndTrace_When_Tracing_Only_High_Level()
{
// Arrange
TestTraceWriter traceWriter = new TestTraceWriter();
traceWriter.TraceSelector = (rqst, category, level) => level >= TraceLevel.Error;
HttpRequestMessage request = new HttpRequestMessage();
bool invoked = false;
// Act
traceWriter.TraceBeginEnd(request,
"",
TraceLevel.Info,
"",
"",
beginTrace: (tr) => { },
execute: () => { },
endTrace: (tr) => { invoked = true; },
errorTrace: (tr) => { });
// Assert
Assert.False(invoked);
}
示例5: 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);
}
示例6: TraceBeginEnd_Invokes_EndTrace
public void TraceBeginEnd_Invokes_EndTrace()
{
// Arrange
TestTraceWriter traceWriter = new TestTraceWriter();
HttpRequestMessage request = new HttpRequestMessage();
bool invoked = false;
// Act
traceWriter.TraceBeginEnd(request,
"",
TraceLevel.Off,
"",
"",
beginTrace: (tr) => { },
execute: () => { },
endTrace: (tr) => { invoked = true; },
errorTrace: (tr) => { });
// Assert
Assert.True(invoked);
}
示例7: TraceBeginEnd_Does_Not_Invoke_EndTrace_When_Error_Occurs
public void TraceBeginEnd_Does_Not_Invoke_EndTrace_When_Error_Occurs()
{
// Arrange
TestTraceWriter traceWriter = new TestTraceWriter();
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) => { invoked = true; },
errorTrace: (tr) => { }));
// Assert
Assert.False(invoked);
Assert.Same(exception, thrown);
}
示例8: TraceBeginEnd_Accepts_Null_Trace_Actions
public void TraceBeginEnd_Accepts_Null_Trace_Actions()
{
// Arrange
TestTraceWriter traceWriter = new TestTraceWriter();
HttpRequestMessage request = new HttpRequestMessage();
// Act & Assert
traceWriter.TraceBeginEnd(request,
"",
TraceLevel.Off,
"",
"",
beginTrace: null,
execute: () => { },
endTrace: null,
errorTrace: null);
}
示例9: TraceBeginEnd_Throws_With_Null_Execute_Action
public void TraceBeginEnd_Throws_With_Null_Execute_Action()
{
// Arrange
TestTraceWriter traceWriter = new TestTraceWriter();
HttpRequestMessage request = new HttpRequestMessage();
// Act & Assert
Assert.ThrowsArgumentNull(() => traceWriter.TraceBeginEnd(request,
"",
TraceLevel.Off,
"",
"",
beginTrace: null,
execute: null,
endTrace: null,
errorTrace: null),
"execute");
}
示例10: TraceBeginEnd_Does_Not_Trace_HttpResponseException_When_Tracing_Only_Higher_Level
public void TraceBeginEnd_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.TraceBeginEnd(request,
"",
TraceLevel.Info,
"",
"",
beginTrace: (tr) => { invoked = true; },
execute: () => { throw exception; },
endTrace: (tr) => { },
errorTrace: (tr) => { }));
// Assert
Assert.False(invoked);
Assert.Empty(traceWriter.Traces);
Assert.Same(thrown, exception);
}
示例11: 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);
}
示例12: TraceBeginEnd_Traces_And_Throws_HttpResponseException
public void TraceBeginEnd_Traces_And_Throws_HttpResponseException()
{
// Arrange
TestTraceWriter traceWriter = new TestTraceWriter();
HttpRequestMessage request = new HttpRequestMessage();
HttpResponseException exception = new HttpResponseException(Net.HttpStatusCode.NotFound);
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 = exception, Message = "errorMessage",
Status = Net.HttpStatusCode.NotFound },
};
// Act
Exception thrown = Assert.Throws<HttpResponseException>(
() => traceWriter.TraceBeginEnd(request,
"testCategory",
TraceLevel.Error,
"tester",
"testOp",
beginTrace: (tr) => { tr.Message = "beginMessage"; },
execute: () => { throw exception; },
endTrace: (tr) => { tr.Message = "won't Happen"; },
errorTrace: (tr) => { tr.Message = "errorMessage"; }));
// Assert
Assert.Equal<TraceRecord>(expectedTraces, traceWriter.Traces, new TraceRecordComparer());
Assert.Same(thrown, exception);
}