我有.NET Core 1.1
API,并且正在按以下方式处理startup.cs中的错误。我正在使用Serilog
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, IApplicationLifetime appLifetime, IRequestContext requestContext)
{
loggerFactory.AddSerilog();
// Ensure any buffered events are sent at shutdown
appLifetime.ApplicationStopped.Register(Log.CloseAndFlush);
var logger = loggerFactory.CreateLogger<Startup>();
app.UseExceptionHandler(
options =>
{
options.Run(
async context =>
{
context.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
var ex = context.Features.Get<IExceptionHandlerFeature>();
if (ex != null)
{
var errmsg = "An unexpected error has occured in API.";
var logDetails = new
{
CorrelationId = requestContext.CorrelationId,
Message = errmsg
};
logger.LogError(1001, ex.Error, "{@LogDetails}", logDetails);
await context.Response.WriteAsync(errmsg).ConfigureAwait(false);
}
});
});
app.UseMvc();
logger.LogInformation("Application has started in environment {0}", env.EnvironmentName);
}
在大多数情况下,发生任何异常时,Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware
会按预期记录异常,但是有时我在日志中看到以下异常
2018-09-21 18:36:01.670 +00:00 [错误]有未处理的异常 发生:程序集中的类型名称重复。
System.ArgumentException:程序集中的类型名称重复。
在System.Reflection.Emit.ModuleBuilder.CheckTypeNameConflict(String strTypeName,类型为enclosingType),位于 System.Reflection.Emit.AssemblyBuilderData.CheckTypeNameConflict(String strTypeName,TypeBuilder enclosingType)位于 System.Reflection.Emit.TypeBuilder.Init(字符串全名, TypeAttributes attr,父类型,Type []接口,ModuleBuilder 模块,PackingSize iPackingSize,Int32 iTypeSize,TypeBuilder enclosingType) System.Reflection.Emit.ModuleBuilder.DefineType(字符串名称, TypeAttributes attr,父类型,Type []接口)在 Microsoft.Extensions.DiagnosticAdapter.Internal.ProxyAssembly.DefineType(String 名称,TypeAttributes属性,Type baseType,Type []接口)
在 Microsoft.Extensions.DiagnosticAdapter.Internal.ProxyTypeEmitter.GenerateProxyTypeFromProperties(Type sourceType,类型targetType,VerificationResult VerificationResult)
在 Microsoft.Extensions.DiagnosticAdapter.Internal.ProxyTypeEmitter.VerifyProxySupport(ProxyBuilderContext 上下文,元组2 key) at Microsoft.Extensions.DiagnosticAdapter.Internal.ProxyTypeEmitter.GetProxyType(ProxyTypeCache cache, Type targetType, Type sourceType) at Microsoft.Extensions.DiagnosticAdapter.Internal.ProxyFactory.CreateProxy[TProxy](Object obj) at Proxy_Method_From_<>f__AnonymousType0
3_To_Void OnBeforeAction(Microsoft.AspNetCore.Http.HttpContext, IRouteData)(Object,Object,IProxyFactory)位于 Microsoft.Extensions.DiagnosticAdapter.DiagnosticSourceAdapter.Write(String diagnosticName,对象参数)位于 Microsoft.Extensions.DiagnosticAdapter.DiagnosticSourceAdapter.System.IObserver> .OnNext(KeyValuePair`2 值)在System.Diagnostics.DiagnosticListener.Write(String 名称,对象值) Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.d__20.MoveNext()---从上一个引发异常的位置开始的堆栈结束--- System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务 任务) Microsoft.AspNetCore.Builder.RouterMiddleware.d__4.MoveNext()
---从上一个引发异常的位置开始的堆栈结束--- System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务 任务) Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware.d__6.MoveNext()
因此,看起来Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware
是它在记录时自我引发的异常
更新1
我有以下参考文献。不知道哪个引用导致了此问题。
Microsoft.ApplicationInsights.AspNetCore
是2.0
,但是此软件包可用的唯一选项是1.0.*
或2.*.*
。我看不到此软件包有1.1.*
个可用