该过程无法访问文件' performance-5-2022.xlsx'因为另一个过程正在使用它
我有一个绩效方面方法,可以记录所有服务需要多长时间。 我将所有服务的响应时间打印到使用封闭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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论