问题
I have .NET Core 1.1
API and i am handling the error in startup.cs as below. I am using 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);
}
Most of the time when there is any exception, Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware
logs the exception as expected however, sometimes i see the following exception in the logs
2018-09-21 18:36:01.670 +00:00 [Error] An unhandled exception has occurred: Duplicate type name within an assembly.
System.ArgumentException: Duplicate type name within an assembly.
at System.Reflection.Emit.ModuleBuilder.CheckTypeNameConflict(String strTypeName, Type enclosingType) at System.Reflection.Emit.AssemblyBuilderData.CheckTypeNameConflict(String strTypeName, TypeBuilder enclosingType) at System.Reflection.Emit.TypeBuilder.Init(String fullname, TypeAttributes attr, Type parent, Type[] interfaces, ModuleBuilder module, PackingSize iPackingSize, Int32 iTypeSize, TypeBuilder enclosingType) at System.Reflection.Emit.ModuleBuilder.DefineType(String name, TypeAttributes attr, Type parent, Type[] interfaces) at Microsoft.Extensions.DiagnosticAdapter.Internal.ProxyAssembly.DefineType(String name, TypeAttributes attributes, Type baseType, Type[] interfaces)
at Microsoft.Extensions.DiagnosticAdapter.Internal.ProxyTypeEmitter.GenerateProxyTypeFromProperties(Type sourceType, Type targetType, VerificationResult verificationResult)
at Microsoft.Extensions.DiagnosticAdapter.Internal.ProxyTypeEmitter.VerifyProxySupport(ProxyBuilderContext context, Tuple2 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 ) at Microsoft.Extensions.DiagnosticAdapter.DiagnosticSourceAdapter.Write(String diagnosticName, Object parameters) at Microsoft.Extensions.DiagnosticAdapter.DiagnosticSourceAdapter.System.IObserver>.OnNext(KeyValuePair`2 value) at System.Diagnostics.DiagnosticListener.Write(String name, Object value) at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.d__20.MoveNext()--- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.AspNetCore.Builder.RouterMiddleware.d__4.MoveNext()
--- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware.d__6.MoveNext()
So looks like Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware
its selft throwing exception while logging
Update 1
I have the following references. Not sure which reference is causing this issue.
Microsoft.ApplicationInsights.AspNetCore
is 2.0
however the only options available for this package are 1.0.*
or 2.*.*
. I don't see this package has 1.1.*
available
来源:https://stackoverflow.com/questions/52450695/duplicate-type-name-within-an-assembly-in-microsoft-aspnetcore-diagnostics-excep