title: Error message when writing stream in file c#
body: <p>I created a function which write an XML file on a specific folder.</p>
  <pre><code>if (!Utils.WriteFile((Constants.PRED_STORAGE_PATH + predictionResponse.EmailId + &quot;.xml&quot;), prediction))
      throw new Exception(&quot;File already exists&quot;);
  </code></pre>
  <p>The writeFile method is the following:</p>
  <pre><code>public static bool WriteFile(string path, Prediction prediction)
  {
      if (File.Exists(path))
          return false;
  
      if (prediction == null)
          throw new Exception(&quot;No prediction was returned&quot;);
  
      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;
  }
  </code></pre>
  <p>I close and
  I have 2 issues concerning this code:</p>
  <ol>
  <li>The file is written in the right folder but it generates an error message</li>
  <li>This code is in a dll but the path of the error message is related to the path of the project not of the dll</li>
  </ol>
  <p>Here is my error message:</p>
  <pre><code>[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:\Users\me\Documents\projects\myproject\utils\Utils.cs:line 71
     at myproject.Controllers.WebhookController.GetWebhook(Object webhookResponse) in CC:\Users\me\Documents\projects\myproject\Controllers\WebhookController.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&amp; next, Scope&amp; scope, Object&amp; state, Boolean&amp; 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&amp; next, Scope&amp; scope, Object&amp; state, Boolean&amp; isCompleted)
     at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeInnerFilterAsync()
  --- End of stack trace from previous location ---
     at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.&lt;InvokeNextResourceFilter&gt;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&amp; next, Scope&amp; scope, Object&amp; state, Boolean&amp; isCompleted)
     at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeFilterPipelineAsync()
  --- End of stack trace from previous location ---
     at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.&lt;InvokeAsync&gt;g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope)
     at Microsoft.AspNetCore.Routing.EndpointMiddleware.&lt;Invoke&gt;g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger)
     at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
     at Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware.&lt;Invoke&gt;g__Awaited|6_0(ExceptionHandlerMiddleware middleware, HttpContext context, Task task)
  [2021-08-24 07:53:15+00:00] [Error] Connection ID &quot;16861477006485751766&quot;, Request ID &quot;800007d7-0000-ea00-b63f-84710c7967bb&quot;: 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:\Users\me\Documents\projects\myproject\utils\Utils.cs:line 71
     at myproject.Controllers.WebhookController.GetWebhook(Object webhookResponse) in C:\Users\me\Documents\projects\myproject\Controllers\WebhookController.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&amp; next, Scope&amp; scope, Object&amp; state, Boolean&amp; 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&amp; next, Scope&amp; scope, Object&amp; state, Boolean&amp; isCompleted)
     at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeInnerFilterAsync()
  --- End of stack trace from previous location ---
     at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.&lt;InvokeNextResourceFilter&gt;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&amp; next, Scope&amp; scope, Object&amp; state, Boolean&amp; isCompleted)
     at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeFilterPipelineAsync()
  --- End of stack trace from previous location ---
     at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.&lt;InvokeAsync&gt;g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope)
     at Microsoft.AspNetCore.Routing.EndpointMiddleware.&lt;Invoke&gt;g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger)
     at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
     at Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware.&lt;Invoke&gt;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.&lt;Invoke&gt;g__Awaited|6_0(ExceptionHandlerMiddleware middleware, HttpContext context, Task task)
     at Microsoft.AspNetCore.Server.IIS.Core.IISHttpContextOfT`1.ProcessRequestAsync()
  </code></pre>