Closed sspl-vssaini closed 2 years ago
Hi @sspl-vssaini, thanks for dropping by.
I would guess that multiple independent processes are running concurrently; the durable log shipping mechanism requires that only one process at a time touches the file set. How are you deploying/hosting the app? Azure App Service?
Hi @nblumhardt
Yes! I too believe that's happening. Since we are using Hangfire for creating jobs, and those jobs execute methods from services. These jobs can/will/might run in parallel or together at a time.
Here is a sample code to give an idea about our case -
public class PService : IPService
{
private readonly ILogService _logService;
public PService(ILogService logService)
{
_logService = logService;
}
// Method that will be run by Hangfire job
public async Task GetEmployees()
{
// Code removed for brevity
var employees = GetEmployeesFromCompany("Company A");
}
// Method that will be run by another Hangfire job
public async Task CreateEmployees()
{
// Code removed for brevity
var employees = await CreateEmployeesInCompany("Company A");
}
private async Task<List<Employee>> GetEmployeesFromCompany(string companyName)
{
Guid companyUid= Guid.Empty;
Guid userUid = Guid.Empty;
_logService.LogInformation("Fetching employees from company {CompanyName}",companyUid, userUid, companyName);
// Assume some long-running task here
}
private async Task<List<Employee>> CreateEmployeesInCompany(string companyName)
{
Guid companyUid= Guid.Empty;
Guid userUid = Guid.Empty;
_logService.LogInformation("Creating employees in company {CompanyName}",companyUid, userUid, companyName);
// Assume some long-running task here
}
}
So what's the recommendation for such a case?
We are using Continuous Deployment for our Azure AppService.
@nblumhardt
Can you please provide any hint or anything that I can look into for resolving this issue?
Using direct HTTP, rather than buffering, seems like the more reliable option in the kind of hosting environment you're describing.
Eventually, it was an issue caused by the Hangfire configuration.
I read from https://github.com/HangfireIO/Hangfire/issues/697#issuecomment-255574396 where the author suggested
I'd also like to highly discourage you from using static job managers (
RecurringJob.AddOrUpdate
,BackgroundJob.Enqueue
) andGlobalConfiguration
in .NET Core.
GlobalConfiguration
methods excessively use static properties for storing configured values (e.g.JobActivator.Current
,JobStorage.Current
etc.). Static classes from above know nothing about DI, and create their own non-DI instances of clients. Which would only be half bad if Hangfire.AspNetCore bootstrapper not registered those static configuration properties as singletone services!So some service instances could already be activated with configured values (for instance, JobActivator from your example), and then you call
.UseActivator(new DotNetCoreActivator(serviceProvider));
and changeJobActivator.Current
for those non-DI instantiates, while DI ones will continue using the old one.It is a mess :)
So you'd better avoid using those static methods at all, and better use
IRecurringJobManager
andIBackgroundJobClient
from DI. And configure everything (including activator and database connection) inside AddHangfire() callback in ConfigureServices, while it hasn't started creating DI services yet.
And I was using these all static methods. So, I started using IBackgroundJobClient
and IRecurringJobManager
and the issue got resolved.
I'm not exactly sure if this is the right place to post or not. But it is an issue.
We have configured the Serilog logger below way in the main project -
Below is the configuration in appsettings.json
In another project, I am logging from various service classes and here is code -
So, a few of our logs frequently get skip. When I check the SerilogInternalErrors.log file as in the screenshot
,
I found the repetition of the below error logs with the file name being changed
Now, I believe I am using Serilog logger with a Singleton pattern. But I think for some reason the Serilog logger is being used by multiple threads and that's causing the issue.
Can you please suggest a solution for this or if I am doing something wrong?