The process cannot access the file 'Performance-5-2022.xlsx' because it is being used by another process

I have a performance aspect method that logs how long it takes all services to respond. I print the response times of all services to an excel file with closedxml.

When running in debug mode on my local computer, the application did not cause any problems, but when I run it on my plesk server, I get the following error.

The process cannot access the file '/var/www/vhosts/domain.com/api.domain.com/Log/Performance-5-2022.xlsx' because it is being used by another process.

public class PerformanceAspect : MethodInterception
    {
        private int _interval;
        private Stopwatch _stopwatch;

        public PerformanceAspect(int interval)
        {
            _interval = interval;
            _stopwatch = ServiceTool.ServiceProvider.GetService<Stopwatch>();
        }

        protected override void OnBefore(IInvocation invocation)
        {
            _stopwatch.Start();
        }

        protected override void OnAfter(IInvocation invocation)
        {
            var contentRoot = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
            var today = DateTime.Now;

            var xlsxName = "Performance-" + today.Month + "-" + today.Year + ".xlsx";

            var fileLocation = contentRoot + "/Log/" + xlsxName;

            var fileExists = File.Exists(fileLocation);

            if (_stopwatch.Elapsed.TotalSeconds > _interval)
            {
                // Debug.WriteLine($"Performance : {invocation.Method.DeclaringType.FullName}.{invocation.Method.Name}--->{_stopwatch.Elapsed.TotalSeconds}");

                using (var workbook = fileExists ? new XLWorkbook(fileLocation) : new XLWorkbook())
                {
                    var worksheet = fileExists ? workbook.Worksheet(1) : workbook.Worksheets.Add("Performance");
                    var currentRow = fileExists ? worksheet.LastRowUsed().RowNumber() : 1;

                    if (fileExists == false)
                    {

                        worksheet.Cell(currentRow, 1).Value = "FullName";
                        worksheet.Cell(currentRow, 2).Value = "Method";
                        worksheet.Cell(currentRow, 3).Value = "Date";
                        worksheet.Cell(currentRow, 4).Value = "TotalSeconds";

                    }


                    currentRow++;
                    worksheet.Cell(currentRow, 1).Value = invocation.Method.DeclaringType.FullName;
                    worksheet.Cell(currentRow, 2).Value = invocation.Method.Name;
                    worksheet.Cell(currentRow, 3).Value = today.ToString();
                    worksheet.Cell(currentRow, 4).Value = _stopwatch.Elapsed.TotalSeconds;

                    workbook.SaveAs(fileLocation);

                }

            }

            _stopwatch.Reset();
        }
    }

where am I doing wrong?

How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum