该过程无法访问文件' performance-5-2022.xlsx'因为另一个过程正在使用它

发布于 2025-01-26 10:51:34 字数 2622 浏览 4 评论 0原文

我有一个绩效方面方法,可以记录所有服务需要多长时间。 我将所有服务的响应时间打印到使用封闭XML的Excel文件中。

在我本地计算机上以调试模式运行时,该应用程序不会引起任何问题,但是当我在Plesk Server上运行它时,我会收到以下错误。

该过程无法访问文件 '/var/www/vhosts/domain.com/api.domain.com/log/performance-5-2022.xlsx' 因为它是由另一个过程使用的。

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();
        }
    }

我在哪里做错?

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(
quot;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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文