-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathAzureFileSystemAsyncTaskTarget.cs
82 lines (69 loc) · 2.35 KB
/
AzureFileSystemAsyncTaskTarget.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#region
using System;
using System.IO;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Azure.Storage;
using Microsoft.Azure.Storage.File;
using NLog.Common;
using NLog.Config;
using NLog.Layouts;
using NLog.Targets;
#endregion
namespace NLog.Extensions.AzureFileSystem
{
[Target("AzureFileSystemAsync")]
public class AzureFileSystemAsyncTaskTarget : AsyncTaskTarget, IAzureFileSystemTarget
{
private readonly FileShareManager _fileShareManager;
public AzureFileSystemAsyncTaskTarget()
{
_fileShareManager = new FileShareManager(this);
IncludeEventProperties = true;
}
/// <summary>
/// Azure storage connection string
/// </summary>
[RequiredParameter]
public string StorageConnectionString { get; set; }
/// <summary>
/// Azure file system share name
/// </summary>
[RequiredParameter]
public string AzureFileShareName { get; set; }
/// <summary>
/// Folder in which the logs are created
/// </summary>
[RequiredParameter]
public Layout AzureFileShareFolder { get; set; }
/// <summary>
/// File name pattern for log files
/// </summary>
[RequiredParameter]
public Layout AzureFileName { get; set; }
#region Overrides of AsyncTaskTarget
/// <inheritdoc />
protected override async Task WriteAsyncTask(LogEventInfo logEvent, CancellationToken cancellationToken)
{
if (string.IsNullOrEmpty(logEvent.Message))
{
return;
}
var folderName = RenderLogEvent(AzureFileShareFolder, logEvent);
var fileName = RenderLogEvent(AzureFileName, logEvent);
var layoutMessage = Layout.Render(logEvent);
var logMessage = string.Concat(layoutMessage, Environment.NewLine);
try
{
await _fileShareManager.LogMessageToAzureFileAsync(logMessage, folderName, fileName).ConfigureAwait(false);
}
catch (StorageException ex)
{
InternalLogger.Error(ex, "AzureFileSystemTarget: failed writing to file: {0} in folder: {1}", fileName,
folderName);
}
}
#endregion
}
}