npgsql / npgsql

Npgsql is the .NET data provider for PostgreSQL.
http://www.npgsql.org
PostgreSQL License
3.3k stars 819 forks source link

An error occurred while writing to logger #4324

Closed Mr-Pearce closed 1 year ago

Mr-Pearce commented 2 years ago

Steps to reproduce

Random csproj nugets

    <ItemGroup>
        <PackageReference Include="AspNetCore.Authentication.ApiKey" Version="5.1.0" />
        <PackageReference Include="MediaTypeMap.Core" Version="2.3.3" />
        <PackageReference Include="NLog.Web.AspNetCore" Version="4.14.0" />
        <PackageReference Include="Npgsql" Version="5.0.12" />
        <PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="5.0.10" />
        <PackageReference Include="RestSharp" Version="106.15.0" />
        <PackageReference Include="Blazorise.Bootstrap" Version="0.9.4.8" />
        <PackageReference Include="Blazorise.Icons.FontAwesome" Version="0.9.4.8" />
        <PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="5.0.14" />
        <PackageReference Include="Microsoft.Extensions.Hosting.WindowsServices" Version="5.0.1" />
        <PackageReference Include="Services.Shared" Version="1.0.5.34" />
    </ItemGroup>

The issue

I Made an update to my Windows Service and since then some testers random gets this error. But its usually gone after i restart the service manually there. (but cant do it on hundreds of customers)

Note my Npgsql Version is 5.0.12.0 and not 5.0.10.0 I Also tried removing logging.AddEventLog(); from ConfigureLogging(last lines of error log indicates it) but it still happened.

ExceptionType: System.AggregateException
Message: An error occurred while writing to logger(s). (Could not load file or assembly 'Npgsql, Version=5.0.10.0, Culture=neutral, PublicKeyToken=5d8b90d52f46fda7'. Der Prozess kann nicht auf die Datei zugreifen, da sie von einem anderen Prozess verwendet wird. (0x80070020))

Stack: at Microsoft.Extensions.Logging.Logger.ThrowLoggingError(List`1 exceptions)
   at Microsoft.Extensions.Logging.Logger.Log[TState](LogLevel logLevel, EventId eventId, TState state, Exception exception, Func`3 formatter)
   at Microsoft.Extensions.Logging.Logger`1.Microsoft.Extensions.Logging.ILogger.Log[TState](LogLevel logLevel, EventId eventId, TState state, Exception exception, Func`3 formatter)
   at Microsoft.Extensions.Logging.LoggerExtensions.Log(ILogger logger, LogLevel logLevel, EventId eventId, Exception exception, String message, Object[] args)
   at Microsoft.Extensions.Logging.LoggerExtensions.LogError(ILogger logger, Exception exception, String message, Object[] args)
   at Services.Shared.Controllers.BaseController`1.ReturnError[TErrorType](Int32 errorType, Exception exception)
   at AbdataService.Controllers.Server.ServerController.ResetCaches() in C:\buildagent\_work\45\s\AbdataService\Controllers\Server\ServerController.cs:line 171
   at lambda_method85(Closure , Object )
   at Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor.TaskOfActionResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeActionMethodAsync>g__Awaited|12_0(ControllerActionInvoker invoker, ValueTask`1 actionResultValueTask)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeNextActionFilterAsync>g__Awaited|10_0(ControllerActionInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Rethrow(ActionExecutedContextSealed context)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeInnerFilterAsync>g__Awaited|13_0(ControllerActionInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeNextExceptionFilterAsync>g__Awaited|25_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
----------------------------------------------------------------------

Inner Exception

ExceptionType: System.IO.FileLoadException
Message: Could not load file or assembly 'Npgsql, Version=5.0.10.0, Culture=neutral, PublicKeyToken=5d8b90d52f46fda7'. Der Prozess kann nicht auf die Datei zugreifen, da sie von einem anderen Prozess verwendet wird. (0x80070020)

Stack: at System.ModuleHandle.ResolveType(QCallModule module, Int32 typeToken, IntPtr* typeInstArgs, Int32 typeInstCount, IntPtr* methodInstArgs, Int32 methodInstCount, ObjectHandleOnStack type)
   at System.ModuleHandle.ResolveTypeHandleInternal(RuntimeModule module, Int32 typeToken, RuntimeTypeHandle[] typeInstantiationContext, RuntimeTypeHandle[] methodInstantiationContext)
   at System.Reflection.RuntimeModule.ResolveType(Int32 metadataToken, Type[] genericTypeArguments, Type[] genericMethodArguments)
   at System.Reflection.CustomAttribute.FilterCustomAttributeRecord(MetadataToken caCtorToken, MetadataImport& scope, RuntimeModule decoratedModule, MetadataToken decoratedToken, RuntimeType attributeFilterType, Boolean mustBeInheritable, ListBuilder`1& derivedAttributes, RuntimeType& attributeType, IRuntimeMethodInfo& ctorWithParameters, Boolean& isVarArg)
   at System.Reflection.CustomAttribute.IsCustomAttributeDefined(RuntimeModule decoratedModule, Int32 decoratedMetadataToken, RuntimeType attributeFilterType, Int32 attributeCtorToken, Boolean mustBeInheritable)
   at System.Reflection.CustomAttribute.IsDefined(RuntimeMethodInfo method, RuntimeType caType, Boolean inherit)
   at System.Diagnostics.StackTrace.ShowInStackTrace(MethodBase mb)
   at System.Diagnostics.StackTrace.ToString(TraceFormat traceFormat, StringBuilder sb)
   at System.Diagnostics.StackTrace.ToString(TraceFormat traceFormat)
   at System.Exception.get_StackTrace()
   at System.IO.FileLoadException.ToString()
   at System.Text.StringBuilder.Append(Object value)
   at Microsoft.Extensions.Logging.EventLog.EventLogLogger.Log[TState](LogLevel logLevel, EventId eventId, TState state, Exception exception, Func`3 formatter)
   at Microsoft.Extensions.Logging.Logger.<Log>g__LoggerLog|12_0[TState](LogLevel logLevel, EventId eventId, ILogger logger, Exception exception, Func`3 formatter, List`1& exceptions, TState& state)
Exception message:
Stack trace:

Further technical details

Npgsql version: Npgsql Version 5.0.12 Npgsql.EntityFrameworkCore.PostgreSQL Version 5.0.10 PostgreSQL version: PostgreSQL 10.4, compiled by Visual C++ build 1800, 64-bit Operating system: windows 10

Other details about my project setup:

roji commented 2 years ago

@Mr-Pearce my best guess is that you have some sort of dependency conflict, where one dependency in your system is build against Npgsql 5.0.10, and the other against 5.0.12 - or some deployment mixup. It doesn't seem to have anything to do with Npgsql itself.

If you can isolate this in a code sample which you can share, I can take a look.

Mr-Pearce commented 2 years ago

Cant really Isolate it or reproduce it. First i thought it would be our installer but in about 10 new installers i tested, every time some test computer gets an error like above and every time its Npgsql.dll (tried in different versions of it)

I Also thought of some version mismatch. I could check the versions on a machine where the bug shows. Its a self containing deployment so all dlls are in the bin folder. The npgsql dll was 5.0.12, there was no 5.0.10 also error above says (translated) "The process cannot access the file because it is being used by another process." I checked the process Explorer but only the current version was open. Could it be the Npgsql.EntityFrameworkCore.PostgreSQL" Version="5.0.10" it Depends on Npgsql(>= 5.0.10)

When i check the versions with ([system.reflection.assembly]::loadfile("C:\pathtodll\Npgsql.dll")).FullName i get Npgsql, Version=5.0.12.0, Culture=neutral, PublicKeyToken=5d8b90d52f46fda7 Npgsql.EntityFrameworkCore.PostgreSQL, Version=5.0.10.0, Culture=neutral, PublicKeyToken=5d8b90d52f46fda7 both not really match with my error

roji commented 1 year ago

Closing for age, and since our logging implementation has substantially changed in more recent versions. If the error still occurs on newer versions, please open a new issue.