Given the following method signature in a WCF service:

```
public string Query(string request)
{
    using (Log.BeginTimedOperation("Persist request"))
    {
        var messageCorrelationId = Guid.NewGuid().ToString();

        var payloadURI = PayloadHelper.GetFullPath(messageCorrelationId);

        PayloadHelper.PersistPayloadWithPath(request, payloadURI);

        Log.Information("My service request {MessageCorrelationId} {RequestPayloadPath}", messageCorrelationId, payloadURI);
    }

    // DoWork here, code removed for brevity

    return result;
}

```

and the corresponding extension methods:

```
public static string GetFullPath(string messageCorrelationId)
{            
    var folderDate = DateTime.Now.ToString("yyyyMMdd");
    var folderHour = DateTime.Now.ToString("HH");
    var logFolder = Path.Combine(ConfigurationManager.AppSettings["NetworkFiler"], "Payloads", folderDate, folderHour);
    Directory.CreateDirectory(logFolder);
    var fileName = $"{messageCorrelationId}-{"MyWCFService"}-{$"{DateTime.Now:yyyy-MM-dd-HH-mm-ss-fff}-{Guid.NewGuid():N}"}.{"xml"}";
    return Path.Combine(logFolder, fileName);
}

public static void PersistPayloadWithPath(string text, string path)
{
    var task = WriteFileAsync(path, text);
    task.GetAwaiter().GetResult();
}

private static async Task WriteFileAsync(string path, string text)
{
    try
    {
        byte[] buffer = Encoding.Unicode.GetBytes(text);
        using (var stream = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.None, bufferSize: 4096, useAsync: true))
        {
            await stream.WriteAsync(buffer, 0, buffer.Length).ConfigureAwait(false);
        }
    }
    catch (Exception ex)
    {
        Serilog.Log.Error(ex, "WriteFileAsync");
    }
}

```

This code will block, if for instance the file is being interrogated by anti-virus (guess) or IO slowness to the filer. 

So here is the great debate, calling a asynchronous method from a synchronous method in ASP.NET. To this day I still don't know if there is a reliable way to create a fire and forget mechanism. It's not that I don't care about the failure, I do, that 'should be' handled by the catch statement and the static Serilog instance.

While writing this version of the post, it dawned on me that maybe one of the problems might be in fact the logger and it's async wrapper around the File Sink.. will test that in a few.

Any help is appreciated with this bugger.

Thank you,
Stephen

**UPDATE-ASYNC**

```
public async Task<string> QueryAsync(string request)
{
    using (Log.BeginTimedOperation("PersistAsync-Request"))
    {
        var messageCorrelationId = Guid.NewGuid().ToString();

        var payloadURI = PayloadHelper.GetFullPath(messageCorrelationId);

        await PayloadHelper.PersistPayloadWithPathAsync(request, payloadURI).ConfigureAwait(false);

        Log.Information("My service request {MessageCorrelationId} {RequestPayloadPath}", messageCorrelationId, payloadURI);
    }

    // DoWork here, code removed for brevity

    return result;
}

public static string GetFullPath(string messageCorrelationId)
{            
    var folderDate = DateTime.Now.ToString("yyyyMMdd");
    var folderHour = DateTime.Now.ToString("HH");
    var logFolder = Path.Combine(ConfigurationManager.AppSettings["NetworkFiler"], "Payloads", folderDate, folderHour);
    Directory.CreateDirectory(logFolder);
    var fileName = $"{messageCorrelationId}-MyWCFService-{DateTime.Now:yyyy-MM-dd-HH-mm-ss-fff}-{Guid.NewGuid():N}.xml";
    return Path.Combine(logFolder, fileName);
}

public static async Task PersistPayloadWithPathAsync(string text, string path)
{
    await WriteFileAsync(path, text).ConfigureAwait(false);
}

private static async Task WriteFileAsync(string path, string text)
{
    try
    {
        byte[] buffer = Encoding.Unicode.GetBytes(text);
        using (var stream = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.None, bufferSize: 4096, useAsync: true))
        {
            await stream.WriteAsync(buffer, 0, buffer.Length).ConfigureAwait(false);
        }
    }
    catch
    {
        // ignored
    }
}

```

Still blocking randomly, lick every 20-30 requests