在c#文件中写入流时出现错误消息

  • 本文关键字:错误 消息 文件 c# stream
  • 更新时间 :
  • 英文 :


我创建了一个函数,用于在特定文件夹上写入XML文件。

if (!Utils.WriteFile((Constants.PRED_STORAGE_PATH + predictionResponse.EmailId + ".xml"), prediction))
throw new Exception("File already exists");

writeFile方法如下:

public static bool WriteFile(string path, Prediction prediction)
{
if (File.Exists(path))
return false;
if (prediction == null)
throw new Exception("No prediction was returned");
XmlSerializer serializer = new XmlSerializer(typeof(Prediction));
// Create an XmlTextWriter using a FileStream.
Stream fs = new FileStream(path, FileMode.Create);
XmlWriter writer = 
XmlWriter.Create(fs, new XmlWriterSettings { Indent = true, Encoding = Encoding.Unicode });
// Serialize using the XmlTextWriter.
serializer.Serialize(writer, prediction);
writer.Flush();
// writer = null;
writer.Close();

fs.Close();
fs.Dispose();
// fs = null;
fs.Flush();

return true;
}

关闭和关于这段代码,我有两个问题:

  1. 文件被写入正确的文件夹,但它产生一个错误消息
  2. 此代码位于dll中,但错误信息的路径与项目的路径相关,而与dll的路径无关

下面是我的错误提示:

[2021-08-24 07:53:15+00:00] [Error] An unhandled exception has occurred while executing the request.    at System.IO.FileStream.Flush(Boolean flushToDisk)
at System.IO.FileStream.Flush()
at myproject.utils.Utils.WriteFile(String path, Prediction prediction) in C:UsersmeDocumentsprojectsmyprojectutilsUtils.cs:line 71
at myproject.Controllers.WebhookController.GetWebhook(Object webhookResponse) in CC:UsersmeDocumentsprojectsmyprojectControllersWebhookController.cs:line 99
at Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor.SyncObjectResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeActionMethodAsync()
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeNextActionFilterAsync()
--- End of stack trace from previous location ---
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()
--- End of stack trace from previous location ---
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeNextResourceFilter>g__Awaited|24_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResourceExecutedContextSealed context)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeFilterPipelineAsync()
--- End of stack trace from previous location ---
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope)
at Microsoft.AspNetCore.Routing.EndpointMiddleware.<Invoke>g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger)
at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware.<Invoke>g__Awaited|6_0(ExceptionHandlerMiddleware middleware, HttpContext context, Task task)
[2021-08-24 07:53:15+00:00] [Error] Connection ID "16861477006485751766", Request ID "800007d7-0000-ea00-b63f-84710c7967bb": An unhandled exception was thrown by the application.    at System.IO.FileStream.Flush(Boolean flushToDisk)
at System.IO.FileStream.Flush()
at myproject.utils.Utils.WriteFile(String path, Prediction prediction) in C:UsersmeDocumentsprojectsmyprojectutilsUtils.cs:line 71
at myproject.Controllers.WebhookController.GetWebhook(Object webhookResponse) in C:UsersmeDocumentsprojectsmyprojectControllersWebhookController.cs:line 99
at Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor.SyncObjectResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeActionMethodAsync()
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeNextActionFilterAsync()
--- End of stack trace from previous location ---
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()
--- End of stack trace from previous location ---
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeNextResourceFilter>g__Awaited|24_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResourceExecutedContextSealed context)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeFilterPipelineAsync()
--- End of stack trace from previous location ---
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope)
at Microsoft.AspNetCore.Routing.EndpointMiddleware.<Invoke>g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger)
at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware.<Invoke>g__Awaited|6_0(ExceptionHandlerMiddleware middleware, HttpContext context, Task task)
at Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware.HandleException(HttpContext context, ExceptionDispatchInfo edi)
at Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware.<Invoke>g__Awaited|6_0(ExceptionHandlerMiddleware middleware, HttpContext context, Task task)
at Microsoft.AspNetCore.Server.IIS.Core.IISHttpContextOfT`1.ProcessRequestAsync()

我只是注释掉了一行,它解决了我的错误信息。

// fs.Flush();

我仍然想知道为什么我的堆栈消息给了我项目的路径,而不是dll的路径。例如:

C:UsersmeDocumentsprojectsmyprojectutilsUtils.cs:line 71
at myproject.Controllers.WebhookController.GetWebhook(Object webhookResponse)

相关内容

  • 没有找到相关文章

最新更新