本文整理汇总了C#中TraceRecord类的典型用法代码示例。如果您正苦于以下问题:C# TraceRecord类的具体用法?C# TraceRecord怎么用?C# TraceRecord使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
TraceRecord类属于命名空间,在下文中一共展示了TraceRecord类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: DoLog
/// <summary>
/// Override to specify how the trace entry should be logged.
/// </summary>
/// <param name="item">Trace entry</param>
protected override void DoLog(TraceLogItem item)
{
using (var db = new SqlDatabase(_sqlConnectionOrName))
{
var logRecord = new TraceRecord
{
// ReSharper disable PossibleInvalidOperationException
Timestamp = item.TimestampUtc.Value,
// ReSharper restore PossibleInvalidOperationException
Type = (int)item.Type,
OperationType = item.OperationType,
SessionId = item.SessionId,
BusinessTransactionId = item.BusinessTransactionId,
OperationInstanceId = item.OperationInstanceId,
TenantId = item.TenantId,
Message = item.Message,
DetailedMessage = item.DetailedMessage,
ServerName = item.ServerName,
// ReSharper disable PossibleInvalidOperationException
ThreadId = item.ThreadId.Value
// ReSharper restore PossibleInvalidOperationException
};
db.Insert(logRecord);
}
}
示例2: ExecuteBindingAsync_Traces_And_Invokes_Inner
public void ExecuteBindingAsync_Traces_And_Invokes_Inner()
{
// Arrange
Mock<HttpParameterDescriptor> mockParamDescriptor = new Mock<HttpParameterDescriptor>() { CallBase = true };
mockParamDescriptor.Setup(d => d.ParameterName).Returns("paramName");
mockParamDescriptor.Setup(d => d.ParameterType).Returns(typeof(string));
Mock<HttpParameterBinding> mockBinding = new Mock<HttpParameterBinding>(mockParamDescriptor.Object) { CallBase = true };
bool innerInvoked = false;
mockBinding.Setup(
b =>
b.ExecuteBindingAsync(It.IsAny<ModelMetadataProvider>(), It.IsAny<HttpActionContext>(),
It.IsAny<CancellationToken>())).Returns(TaskHelpers.Completed()).Callback(() => innerInvoked = true);
TestTraceWriter traceWriter = new TestTraceWriter();
HttpParameterBindingTracer tracer = new HttpParameterBindingTracer(mockBinding.Object, traceWriter);
HttpActionContext actionContext = ContextUtil.CreateActionContext();
ModelMetadataProvider metadataProvider = new EmptyModelMetadataProvider();
TraceRecord[] expectedTraces = new TraceRecord[]
{
new TraceRecord(actionContext.Request, TraceCategories.ModelBindingCategory, TraceLevel.Info) { Kind = TraceKind.Begin, Operation = "ExecuteBindingAsync" },
new TraceRecord(actionContext.Request, TraceCategories.ModelBindingCategory, TraceLevel.Info) { Kind = TraceKind.End, Operation = "ExecuteBindingAsync" }
};
// Act
Task task = tracer.ExecuteBindingAsync(metadataProvider, actionContext, CancellationToken.None);
task.Wait();
// Assert
Assert.Equal<TraceRecord>(expectedTraces, traceWriter.Traces, new TraceRecordComparer());
Assert.True(innerInvoked);
}
示例3: ExecuteBindingAsync_Traces_And_Throws_When_Inner_Throws
public void ExecuteBindingAsync_Traces_And_Throws_When_Inner_Throws()
{
// Arrange
Mock<HttpParameterDescriptor> mockParamDescriptor = new Mock<HttpParameterDescriptor>() { CallBase = true };
mockParamDescriptor.Setup(d => d.ParameterName).Returns("paramName");
mockParamDescriptor.Setup(d => d.ParameterType).Returns(typeof(string));
Mock<FormatterParameterBinding> mockBinding = new Mock<FormatterParameterBinding>(mockParamDescriptor.Object, new MediaTypeFormatterCollection(), null) { CallBase = true };
InvalidOperationException exception = new InvalidOperationException("test");
mockBinding.Setup(
b =>
b.ExecuteBindingAsync(It.IsAny<ModelMetadataProvider>(), It.IsAny<HttpActionContext>(),
It.IsAny<CancellationToken>())).Throws(exception);
TestTraceWriter traceWriter = new TestTraceWriter();
FormatterParameterBindingTracer tracer = new FormatterParameterBindingTracer(mockBinding.Object, traceWriter);
HttpActionContext actionContext = ContextUtil.CreateActionContext();
ModelMetadataProvider metadataProvider = new EmptyModelMetadataProvider();
TraceRecord[] expectedTraces = new TraceRecord[]
{
new TraceRecord(actionContext.Request, TraceCategories.ModelBindingCategory, TraceLevel.Info) { Kind = TraceKind.Begin, Operation = "ExecuteBindingAsync" },
new TraceRecord(actionContext.Request, TraceCategories.ModelBindingCategory, TraceLevel.Error) { Kind = TraceKind.End, Operation = "ExecuteBindingAsync" }
};
// Act & Assert
Exception thrown = Assert.Throws<InvalidOperationException>(() => tracer.ExecuteBindingAsync(metadataProvider, actionContext, CancellationToken.None));
// Assert
Assert.Same(exception, thrown);
Assert.Same(exception, traceWriter.Traces[1].Exception);
Assert.Equal<TraceRecord>(expectedTraces, traceWriter.Traces, new TraceRecordComparer());
}
示例4: Create_Throws_And_Traces_When_Inner_Throws
public void Create_Throws_And_Traces_When_Inner_Throws()
{
// Arrange
Mock<ApiController> mockController = new Mock<ApiController>();
Mock<IHttpControllerActivator> mockActivator = new Mock<IHttpControllerActivator>() { CallBase = true };
InvalidOperationException exception = new InvalidOperationException("test");
mockActivator.Setup(b => b.Create(It.IsAny<HttpRequestMessage>(), It.IsAny<HttpControllerDescriptor>(), It.IsAny<Type>())).Throws(exception);
HttpRequestMessage request = new HttpRequestMessage();
TestTraceWriter traceWriter = new TestTraceWriter();
HttpControllerActivatorTracer tracer = new HttpControllerActivatorTracer(mockActivator.Object, traceWriter);
TraceRecord[] expectedTraces = new TraceRecord[]
{
new TraceRecord(request, TraceCategories.ControllersCategory, TraceLevel.Info) { Kind = TraceKind.Begin, Operation = "Create" },
new TraceRecord(request, TraceCategories.ControllersCategory, TraceLevel.Error) { Kind = TraceKind.End, Operation = "Create" }
};
// Act & Assert
Exception thrown = Assert.Throws<InvalidOperationException>(() => ((IHttpControllerActivator)tracer).Create(request, controllerDescriptor: null, controllerType: mockController.Object.GetType()));
// Assert
Assert.Equal<TraceRecord>(expectedTraces, traceWriter.Traces, new TraceRecordComparer());
Assert.Same(exception, thrown);
Assert.Same(exception, traceWriter.Traces[1].Exception);
}
示例5: ExecuteExceptionFilterAsync_Traces
public void ExecuteExceptionFilterAsync_Traces()
{
// Arrange
HttpRequestMessage request = new HttpRequestMessage();
HttpResponseMessage response = new HttpResponseMessage();
Mock<ExceptionFilterAttribute> mockAttr = new Mock<ExceptionFilterAttribute>() { CallBase = true };
Mock<HttpActionDescriptor> mockActionDescriptor = new Mock<HttpActionDescriptor>() { CallBase = true };
mockActionDescriptor.Setup(a => a.ActionName).Returns("test");
mockActionDescriptor.Setup(a => a.GetParameters()).Returns(new Collection<HttpParameterDescriptor>(new HttpParameterDescriptor[0]));
HttpActionExecutedContext actionExecutedContext = ContextUtil.GetActionExecutedContext(request, response);
TestTraceWriter traceWriter = new TestTraceWriter();
ExceptionFilterAttributeTracer tracer = new ExceptionFilterAttributeTracer(mockAttr.Object, traceWriter);
TraceRecord[] expectedTraces = new TraceRecord[]
{
new TraceRecord(request, TraceCategories.FiltersCategory, TraceLevel.Info) { Kind = TraceKind.Begin, Operation = "OnException" },
new TraceRecord(request, TraceCategories.FiltersCategory, TraceLevel.Info) { Kind = TraceKind.End, Operation = "OnException" },
};
// Act
Task task = ((IExceptionFilter)tracer).ExecuteExceptionFilterAsync(actionExecutedContext, CancellationToken.None);
task.Wait();
// Assert
Assert.Equal<TraceRecord>(expectedTraces, traceWriter.Traces, new TraceRecordComparer());
}
示例6: ExecuteAuthorizationFilterAsync_Faults_And_Traces_When_Inner_Faults
public void ExecuteAuthorizationFilterAsync_Faults_And_Traces_When_Inner_Faults()
{
// Arrange
Mock<IAuthorizationFilter> mockAttr = new Mock<IAuthorizationFilter>() { CallBase = true };
HttpResponseMessage response = new HttpResponseMessage();
InvalidOperationException exception = new InvalidOperationException("test");
TaskCompletionSource<HttpResponseMessage> tcs = new TaskCompletionSource<HttpResponseMessage>(response);
tcs.TrySetException(exception);
mockAttr.Setup(a => a.ExecuteAuthorizationFilterAsync(It.IsAny<HttpActionContext>(), It.IsAny<CancellationToken>(), It.IsAny<Func<Task<HttpResponseMessage>>>())).Returns(tcs.Task);
Mock<HttpActionDescriptor> mockActionDescriptor = new Mock<HttpActionDescriptor>() { CallBase = true };
mockActionDescriptor.Setup(a => a.ActionName).Returns("test");
mockActionDescriptor.Setup(a => a.GetParameters()).Returns(new Collection<HttpParameterDescriptor>(new HttpParameterDescriptor[0]));
HttpActionContext actionContext = ContextUtil.CreateActionContext(actionDescriptor: mockActionDescriptor.Object);
Func<Task<HttpResponseMessage>> continuation = () => TaskHelpers.FromResult<HttpResponseMessage>(response);
TestTraceWriter traceWriter = new TestTraceWriter();
AuthorizationFilterTracer tracer = new AuthorizationFilterTracer(mockAttr.Object, traceWriter);
TraceRecord[] expectedTraces = new TraceRecord[]
{
new TraceRecord(actionContext.Request, TraceCategories.FiltersCategory, TraceLevel.Info) { Kind = TraceKind.Begin, Operation = "ExecuteAuthorizationFilterAsync" },
new TraceRecord(actionContext.Request, TraceCategories.FiltersCategory, TraceLevel.Error) { Kind = TraceKind.End, Operation = "ExecuteAuthorizationFilterAsync" }
};
// Act & Assert
Task task = ((IAuthorizationFilter)tracer).ExecuteAuthorizationFilterAsync(actionContext, CancellationToken.None, continuation);
Exception thrown = Assert.Throws<InvalidOperationException>(() => task.Wait());
// Assert
Assert.Same(exception, thrown);
Assert.Same(exception, traceWriter.Traces[1].Exception);
Assert.Equal<TraceRecord>(expectedTraces, traceWriter.Traces, new TraceRecordComparer());
}
示例7: ExecuteAsync_Faults_And_Traces_When_Inner_Faults
public void ExecuteAsync_Faults_And_Traces_When_Inner_Faults()
{
// Arrange
InvalidOperationException exception = new InvalidOperationException();
TaskCompletionSource<HttpResponseMessage> tcs = new TaskCompletionSource<HttpResponseMessage>();
tcs.TrySetException(exception);
Mock<ApiController> mockController = new Mock<ApiController>() { CallBase = true };
mockController.Setup(b => b.ExecuteAsync(It.IsAny<HttpControllerContext>(), It.IsAny<CancellationToken>())).Returns(tcs.Task);
HttpControllerContext controllerContext = ContextUtil.CreateControllerContext(request: new HttpRequestMessage());
controllerContext.ControllerDescriptor = _controllerDescriptor;
controllerContext.Controller = mockController.Object;
HttpActionContext actionContext = ContextUtil.CreateActionContext(controllerContext, actionDescriptor: _mockActionDescriptor.Object);
TestTraceWriter traceWriter = new TestTraceWriter();
HttpControllerTracer tracer = new HttpControllerTracer(mockController.Object, traceWriter);
TraceRecord[] expectedTraces = new TraceRecord[]
{
new TraceRecord(actionContext.Request, TraceCategories.ControllersCategory, TraceLevel.Info) { Kind = TraceKind.Begin },
new TraceRecord(actionContext.Request, TraceCategories.ControllersCategory, TraceLevel.Error) { Kind = TraceKind.End }
};
// Act
Exception thrown = Assert.Throws<InvalidOperationException>(() => ((IHttpController)tracer).ExecuteAsync(controllerContext, CancellationToken.None).Wait());
// Assert
Assert.Equal<TraceRecord>(expectedTraces, traceWriter.Traces, new TraceRecordComparer());
Assert.Same(exception, thrown);
Assert.Same(exception, traceWriter.Traces[1].Exception);
}
示例8: ExecuteExceptionFilterAsync_Faults_And_Traces_When_Inner_Faults
public void ExecuteExceptionFilterAsync_Faults_And_Traces_When_Inner_Faults()
{
// Arrange
HttpRequestMessage request = new HttpRequestMessage();
HttpResponseMessage response = new HttpResponseMessage();
Mock<IExceptionFilter> mockFilter = new Mock<IExceptionFilter>() { CallBase = true };
InvalidOperationException exception = new InvalidOperationException("test");
TaskCompletionSource<object> tcs = new TaskCompletionSource<object>(null);
tcs.TrySetException(exception);
mockFilter.Setup(a => a.ExecuteExceptionFilterAsync(It.IsAny<HttpActionExecutedContext>(), It.IsAny<CancellationToken>())).Returns(tcs.Task);
HttpActionExecutedContext actionExecutedContext = ContextUtil.GetActionExecutedContext(request, response);
TestTraceWriter traceWriter = new TestTraceWriter();
ExceptionFilterTracer tracer = new ExceptionFilterTracer(mockFilter.Object, traceWriter);
TraceRecord[] expectedTraces = new TraceRecord[]
{
new TraceRecord(request, TraceCategories.FiltersCategory, TraceLevel.Info) { Kind = TraceKind.Begin, Operation = "ExecuteExceptionFilterAsync" },
new TraceRecord(request, TraceCategories.FiltersCategory, TraceLevel.Error) { Kind = TraceKind.End, Operation = "ExecuteExceptionFilterAsync" }
};
// Act
Task task = ((IExceptionFilter)tracer).ExecuteExceptionFilterAsync(actionExecutedContext, CancellationToken.None);
// Assert
Exception thrown = Assert.Throws<InvalidOperationException>(() => task.Wait());
Assert.Same(exception, thrown);
Assert.Same(exception, traceWriter.Traces[1].Exception);
Assert.Equal<TraceRecord>(expectedTraces, traceWriter.Traces, new TraceRecordComparer());
}
示例9: ExecuteBindingAsync_Traces_And_Invokes_Inner_ReadAsync
public void ExecuteBindingAsync_Traces_And_Invokes_Inner_ReadAsync()
{
// Arrange
Mock<HttpParameterDescriptor> mockParamDescriptor = new Mock<HttpParameterDescriptor>() { CallBase = true };
mockParamDescriptor.Setup(d => d.ParameterName).Returns("paramName");
mockParamDescriptor.Setup(d => d.ParameterType).Returns(typeof (string));
FormatterParameterBinding binding = new FormatterParameterBinding(mockParamDescriptor.Object, new MediaTypeFormatterCollection(), null);
TestTraceWriter traceWriter = new TestTraceWriter();
FormatterParameterBindingTracer tracer = new FormatterParameterBindingTracer(binding, traceWriter);
HttpActionContext actionContext = ContextUtil.CreateActionContext();
actionContext.Request.Content = new StringContent("true");
actionContext.Request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
ModelMetadataProvider metadataProvider = new EmptyModelMetadataProvider();
TraceRecord[] expectedTraces = new TraceRecord[]
{
new TraceRecord(actionContext.Request, TraceCategories.ModelBindingCategory, TraceLevel.Info) { Kind = TraceKind.Begin, Operation = "ExecuteBindingAsync" },
new TraceRecord(actionContext.Request, TraceCategories.ModelBindingCategory, TraceLevel.Info) { Kind = TraceKind.End, Operation = "ExecuteBindingAsync" }
};
// Act
Task task = tracer.ExecuteBindingAsync(metadataProvider, actionContext, CancellationToken.None);
task.Wait();
// Assert
Assert.Equal<TraceRecord>(expectedTraces, traceWriter.Traces, new TraceRecordComparer());
Assert.Equal("True", actionContext.ActionArguments["paramName"]);
}
示例10: SendAsync_Traces_And_Throws_When_Inner_Throws
public void SendAsync_Traces_And_Throws_When_Inner_Throws()
{
// Arrange
InvalidOperationException exception = new InvalidOperationException("test");
MockDelegatingHandler mockHandler = new MockDelegatingHandler((rqst, cancellation) => { throw exception; });
TestTraceWriter traceWriter = new TestTraceWriter();
MessageHandlerTracer tracer = new MessageHandlerTracer(mockHandler, traceWriter);
// DelegatingHandlers require an InnerHandler to run. We create a mock one to simulate what
// would happen when a DelegatingHandler executing after the tracer throws.
MockHttpMessageHandler mockInnerHandler = new MockHttpMessageHandler((rqst, cancellation) => { throw exception; });
tracer.InnerHandler = mockInnerHandler;
HttpRequestMessage request = new HttpRequestMessage();
TraceRecord[] expectedTraces = new TraceRecord[]
{
new TraceRecord(request, TraceCategories.MessageHandlersCategory, TraceLevel.Info) { Kind = TraceKind.Begin, Operation = "SendAsync" },
new TraceRecord(request, TraceCategories.MessageHandlersCategory, TraceLevel.Error) { Kind = TraceKind.End, Operation = "SendAsync" }
};
MethodInfo method = typeof(DelegatingHandler).GetMethod("SendAsync",
BindingFlags.Public | BindingFlags.NonPublic |
BindingFlags.Instance);
// Act
Exception thrown =
Assert.Throws<TargetInvocationException>(
() => method.Invoke(tracer, new object[] { request, CancellationToken.None }));
// Assert
Assert.Equal<TraceRecord>(expectedTraces, traceWriter.Traces, new TraceRecordComparer());
Assert.Same(exception, thrown.InnerException);
Assert.Same(exception, traceWriter.Traces[1].Exception);
}
示例11: ExecuteExceptionFilterAsync_Traces
public void ExecuteExceptionFilterAsync_Traces()
{
// Arrange
HttpRequestMessage request = new HttpRequestMessage();
HttpResponseMessage response = new HttpResponseMessage();
Mock<IExceptionFilter> mockFilter = new Mock<IExceptionFilter>() { CallBase = true };
mockFilter.Setup(
f => f.ExecuteExceptionFilterAsync(It.IsAny<HttpActionExecutedContext>(), It.IsAny<CancellationToken>())).
Returns(TaskHelpers.Completed());
HttpActionExecutedContext actionExecutedContext = ContextUtil.GetActionExecutedContext(request, response);
TestTraceWriter traceWriter = new TestTraceWriter();
ExceptionFilterTracer tracer = new ExceptionFilterTracer(mockFilter.Object, traceWriter);
TraceRecord[] expectedTraces = new TraceRecord[]
{
new TraceRecord(request, TraceCategories.FiltersCategory, TraceLevel.Info) { Kind = TraceKind.Begin, Operation = "ExecuteExceptionFilterAsync" },
new TraceRecord(request, TraceCategories.FiltersCategory, TraceLevel.Info) { Kind = TraceKind.End, Operation = "ExecuteExceptionFilterAsync" },
};
// Act
Task task = ((IExceptionFilter)tracer).ExecuteExceptionFilterAsync(actionExecutedContext, CancellationToken.None);
task.Wait();
// Assert
Assert.Equal<TraceRecord>(expectedTraces, traceWriter.Traces, new TraceRecordComparer());
}
示例12: OnReadFromStreamAsync_Traces_And_Throws_When_Inner_Throws
public void OnReadFromStreamAsync_Traces_And_Throws_When_Inner_Throws()
{
// Arrange
InvalidOperationException exception = new InvalidOperationException("test");
Mock<MediaTypeFormatter> mockFormatter = new Mock<MediaTypeFormatter>() { CallBase = true };
mockFormatter.Setup(
f => f.ReadFromStreamAsync(It.IsAny<Type>(), It.IsAny<Stream>(), It.IsAny<HttpContentHeaders>(), It.IsAny<IFormatterLogger>())).Throws(exception);
TestTraceWriter traceWriter = new TestTraceWriter();
HttpRequestMessage request = new HttpRequestMessage();
request.Content = new StringContent("");
MediaTypeFormatterTracer tracer = new MediaTypeFormatterTracer(mockFormatter.Object, traceWriter, request);
TraceRecord[] expectedTraces = new TraceRecord[]
{
new TraceRecord(request, TraceCategories.FormattingCategory, TraceLevel.Info) { Kind = TraceKind.Begin, Operation = "ReadFromStreamAsync" },
new TraceRecord(request, TraceCategories.FormattingCategory, TraceLevel.Error) { Kind = TraceKind.End, Operation = "ReadFromStreamAsync" }
};
// Act
Exception thrown = Assert.Throws<InvalidOperationException>(() => tracer.ReadFromStreamAsync(typeof(string), new MemoryStream(), request.Content.Headers, null));
// Assert
Assert.Equal<TraceRecord>(expectedTraces, traceWriter.Traces, new TraceRecordComparer());
Assert.Same(exception, thrown);
Assert.Same(exception, traceWriter.Traces[1].Exception);
}
示例13: SendAsync_Traces_And_Invokes_Inner
public void SendAsync_Traces_And_Invokes_Inner()
{
// Arrange
HttpResponseMessage response = new HttpResponseMessage();
MockDelegatingHandler mockHandler = new MockDelegatingHandler((rqst, cancellation) =>
TaskHelpers.FromResult<HttpResponseMessage>(response));
TestTraceWriter traceWriter = new TestTraceWriter();
MessageHandlerTracer tracer = new MessageHandlerTracer(mockHandler, traceWriter);
MockHttpMessageHandler mockInnerHandler = new MockHttpMessageHandler((rqst, cancellation) =>
TaskHelpers.FromResult<HttpResponseMessage>(response));
tracer.InnerHandler = mockInnerHandler;
HttpRequestMessage request = new HttpRequestMessage();
TraceRecord[] expectedTraces = new TraceRecord[]
{
new TraceRecord(request, TraceCategories.MessageHandlersCategory, TraceLevel.Info) { Kind = TraceKind.Begin, Operation = "SendAsync" },
new TraceRecord(request, TraceCategories.MessageHandlersCategory, TraceLevel.Info) { Kind = TraceKind.End, Operation = "SendAsync" }
};
MethodInfo method = typeof(DelegatingHandler).GetMethod("SendAsync",
BindingFlags.Public | BindingFlags.NonPublic |
BindingFlags.Instance);
// Act
Task<HttpResponseMessage> task = method.Invoke(tracer, new object[] { request, CancellationToken.None }) as Task<HttpResponseMessage>;
HttpResponseMessage actualResponse = task.Result;
// Assert
Assert.Equal<TraceRecord>(expectedTraces, traceWriter.Traces, new TraceRecordComparer());
Assert.Same(response, actualResponse);
}
示例14: OnReadFromStreamAsync_Traces
public void OnReadFromStreamAsync_Traces()
{
// Arrange
Mock<MediaTypeFormatter> mockFormatter = new Mock<MediaTypeFormatter>() { CallBase = true };
mockFormatter.Setup(
f => f.ReadFromStreamAsync(It.IsAny<Type>(), It.IsAny<Stream>(), It.IsAny<HttpContentHeaders>(), It.IsAny<IFormatterLogger>())).
Returns(TaskHelpers.FromResult<object>("sampleValue"));
TestTraceWriter traceWriter = new TestTraceWriter();
HttpRequestMessage request = new HttpRequestMessage();
request.Content = new StringContent("");
MediaTypeFormatterTracer tracer = new MediaTypeFormatterTracer(mockFormatter.Object, traceWriter, request);
TraceRecord[] expectedTraces = new TraceRecord[]
{
new TraceRecord(request, TraceCategories.FormattingCategory, TraceLevel.Info) { Kind = TraceKind.Begin, Operation = "ReadFromStreamAsync" },
new TraceRecord(request, TraceCategories.FormattingCategory, TraceLevel.Info) { Kind = TraceKind.End, Operation = "ReadFromStreamAsync" }
};
// Act
Task<object> task = tracer.ReadFromStreamAsync(typeof(string), new MemoryStream(), request.Content.Headers, null);
string result = task.Result as string;
// Assert
Assert.Equal<TraceRecord>(expectedTraces, traceWriter.Traces, new TraceRecordComparer());
Assert.Equal("sampleValue", result);
}
示例15: ExecuteExceptionFilterAsync_Throws_And_Traces_When_Inner_OnException_Throws
public void ExecuteExceptionFilterAsync_Throws_And_Traces_When_Inner_OnException_Throws()
{
// Arrange
HttpRequestMessage request = new HttpRequestMessage();
HttpResponseMessage response = new HttpResponseMessage();
Mock<ExceptionFilterAttribute> mockAttr = new Mock<ExceptionFilterAttribute>() { CallBase = true };
InvalidOperationException exception = new InvalidOperationException("test");
mockAttr.Setup(a => a.OnException(It.IsAny<HttpActionExecutedContext>())).Throws(exception);
Mock<HttpActionDescriptor> mockActionDescriptor = new Mock<HttpActionDescriptor>() { CallBase = true };
mockActionDescriptor.Setup(a => a.ActionName).Returns("test");
mockActionDescriptor.Setup(a => a.GetParameters()).Returns(new Collection<HttpParameterDescriptor>(new HttpParameterDescriptor[0]));
HttpActionExecutedContext actionExecutedContext = ContextUtil.GetActionExecutedContext(request, response);
TestTraceWriter traceWriter = new TestTraceWriter();
ExceptionFilterAttributeTracer tracer = new ExceptionFilterAttributeTracer(mockAttr.Object, traceWriter);
TraceRecord[] expectedTraces = new TraceRecord[]
{
new TraceRecord(request, TraceCategories.FiltersCategory, TraceLevel.Info) { Kind = TraceKind.Begin, Operation = "OnException" },
new TraceRecord(request, TraceCategories.FiltersCategory, TraceLevel.Error) { Kind = TraceKind.End, Operation = "OnException" }
};
// Act
Exception thrown =
Assert.Throws<InvalidOperationException>(
() => ((IExceptionFilter) tracer).ExecuteExceptionFilterAsync(actionExecutedContext, CancellationToken.None));
// Assert
Assert.Same(exception, thrown);
Assert.Same(exception, traceWriter.Traces[1].Exception);
Assert.Equal<TraceRecord>(expectedTraces, traceWriter.Traces, new TraceRecordComparer());
}