Skip to content
This repository was archived by the owner on Dec 13, 2018. It is now read-only.

Commit dad79f0

Browse files
committed
Adress PR comments
1 parent 2de4588 commit dad79f0

11 files changed

+124
-60
lines changed

src/Microsoft.Extensions.Logging.AzureWebAppDiagnostics/AzureWebAppDiagnosticsFactoryExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public static class AzureWebAppDiagnosticsFactoryExtensions
1717
/// <param name="factory">The extension method argument</param>
1818
public static ILoggerFactory AddAzureWebAppDiagnostics(this ILoggerFactory factory)
1919
{
20-
return AddAzureWebAppDiagnostics(factory, null);
20+
return AddAzureWebAppDiagnostics(factory, new AzureWebAppDiagnosticsSettings());
2121
}
2222

2323
/// <summary>

src/Microsoft.Extensions.Logging.AzureWebAppDiagnostics/AzureWebAppDiagnosticsLoggerProvider.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

4+
using System;
45
using Microsoft.Extensions.Logging.Abstractions.Internal;
56
using Microsoft.Extensions.Logging.AzureWebAppDiagnostics.Internal;
67
using Serilog;
@@ -21,6 +22,11 @@ public class AzureWebAppDiagnosticsLoggerProvider : ILoggerProvider
2122
/// </summary>
2223
public AzureWebAppDiagnosticsLoggerProvider(WebAppContext context, AzureWebAppDiagnosticsSettings settings)
2324
{
25+
if (settings == null)
26+
{
27+
throw new ArgumentNullException(nameof(settings));
28+
}
29+
2430
_configurationReader = new WebAppLogConfigurationReader(context);
2531

2632
var config = _configurationReader.Current;
@@ -32,6 +38,7 @@ public AzureWebAppDiagnosticsLoggerProvider(WebAppContext context, AzureWebAppDi
3238
var fileLoggerProvider = new FileLoggerProvider(
3339
settings.FileSizeLimit,
3440
settings.RetainedFileCountLimit,
41+
settings.BackgroundQueueSize,
3542
settings.OutputTemplate);
3643

3744
_loggerFactory.AddSerilog(fileLoggerProvider.ConfigureLogger(_configurationReader));
@@ -40,8 +47,10 @@ public AzureWebAppDiagnosticsLoggerProvider(WebAppContext context, AzureWebAppDi
4047
var blobLoggerProvider = new AzureBlobLoggerProvider(
4148
settings.OutputTemplate,
4249
context.SiteName,
50+
context.SiteInstanceId,
4351
settings.BlobName,
4452
settings.BlobBatchSize,
53+
settings.BackgroundQueueSize,
4554
settings.BlobCommitPeriod);
4655
_loggerFactory.AddSerilog(blobLoggerProvider.ConfigureLogger(_configurationReader));
4756
}
@@ -57,7 +66,7 @@ public ILogger CreateLogger(string categoryName)
5766
/// <inheritdoc />
5867
public void Dispose()
5968
{
60-
_loggerFactory.Dispose();
69+
_loggerFactory?.Dispose();
6170
_configurationReader.Dispose();
6271
}
6372
}

src/Microsoft.Extensions.Logging.AzureWebAppDiagnostics/AzureWebAppDiagnosticsSettings.cs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,33 +11,36 @@ namespace Microsoft.Extensions.Logging.AzureWebAppDiagnostics
1111
public class AzureWebAppDiagnosticsSettings
1212
{
1313
/// <summary>
14-
/// A strictly positive value representing the maximum log size in bytes. Once the log is full, no more message will be appended
14+
/// Gets or sets a strictly positive value representing the maximum log size in bytes. Once the log is full, no more message will be appended.
1515
/// </summary>
1616
public int FileSizeLimit { get; set; } = 10 * 1024 * 1024;
1717

1818
/// <summary>
19-
/// A strictly positive value representing the maximum retained file count
19+
/// Gets or sets a strictly positive value representing the maximum retained file count.
2020
/// </summary>
2121
public int RetainedFileCountLimit { get; set; } = 2;
2222

2323
/// <summary>
24-
/// A message template describing the output messages
24+
/// Gets or sets a message template describing the output messages.
2525
/// </summary>
2626
public string OutputTemplate { get; set; } = "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level}] {Message}{NewLine}{Exception}";
2727

2828
/// <summary>
29-
/// The maximum number of events to include in a single blob append batch.
29+
/// Gets or sets a maximum number of events to include in a single blob append batch.
3030
/// </summary>
3131
public int BlobBatchSize { get; set; } = 32;
3232

3333
/// <summary>
34-
/// The time to wait between checking for blob log batches
34+
/// Gets or sets a time to wait between checking for blob log batches.
3535
/// </summary>
3636
public TimeSpan BlobCommitPeriod { get; set; } = TimeSpan.FromSeconds(5);
3737

3838
/// <summary>
39-
/// The last section of log blob name.
39+
/// Gets or sets the last section of log blob name.
4040
/// </summary>
4141
public string BlobName { get; set; } = "applicationLog.txt";
42+
43+
/// Gets of sets the maximum size of the background log message queue.
44+
public int BackgroundQueueSize { get; set; }
4245
}
4346
}

src/Microsoft.Extensions.Logging.AzureWebAppDiagnostics/Internal/AzureBlobLoggerProvider.cs

Lines changed: 45 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,47 +10,79 @@
1010
namespace Microsoft.Extensions.Logging.AzureWebAppDiagnostics.Internal
1111
{
1212
/// <summary>
13-
/// The <see cref="SerilogLoggerProvider"/> implemenation that creates instances of <see cref="Serilog.Core.Logger"/> connected to <see cref="AzureBlobSink"/>.
13+
/// The implemenation of logger provider that creates instances of <see cref="Serilog.Core.Logger"/>.
1414
/// </summary>
15-
public class AzureBlobLoggerProvider : SerilogLoggerProvider
15+
public class AzureBlobLoggerProvider
1616
{
1717
private readonly string _outputTemplate;
1818
private readonly string _appName;
19+
private readonly string _instanceId;
1920
private readonly string _fileName;
2021
private readonly int _batchSize;
22+
private readonly int _backgroundQueueSize;
2123
private readonly TimeSpan _period;
2224

2325
/// <summary>
2426
/// Creates a new instance of the <see cref="AzureBlobLoggerProvider"/> class.
2527
/// </summary>
26-
/// <param name="outputTemplate"></param>
27-
/// <param name="appName"></param>
28-
/// <param name="fileName"></param>
29-
/// <param name="batchSize"></param>
30-
/// <param name="period"></param>
31-
public AzureBlobLoggerProvider(string outputTemplate, string appName, string fileName, int batchSize, TimeSpan period)
28+
/// <param name="outputTemplate">A message template describing the output messages</param>
29+
/// <param name="appName">The application name to use in blob name</param>
30+
/// <param name="instanceId">The application instance id to use in blob name</param>
31+
/// <param name="fileName">The last section in log blob name</param>
32+
/// <param name="batchSize">A maximum number of events to include in a single blob append batch</param>
33+
/// <param name="backgroundQueueSize">The maximum size of the background queue</param>
34+
/// <param name="period">A time to wait between checking for blob log batches</param>
35+
public AzureBlobLoggerProvider(string outputTemplate, string appName, string instanceId, string fileName, int batchSize, int backgroundQueueSize, TimeSpan period)
3236
{
37+
if (outputTemplate == null)
38+
{
39+
throw new ArgumentNullException(nameof(outputTemplate));
40+
}
41+
if (appName == null)
42+
{
43+
throw new ArgumentNullException(nameof(appName));
44+
}
45+
if (instanceId == null)
46+
{
47+
throw new ArgumentNullException(nameof(instanceId));
48+
}
49+
if (fileName == null)
50+
{
51+
throw new ArgumentNullException(nameof(fileName));
52+
}
53+
if (batchSize <= 0)
54+
{
55+
throw new ArgumentOutOfRangeException(nameof(batchSize), $"{nameof(batchSize)} should be a positive number.");
56+
}
57+
if (period <= TimeSpan.Zero)
58+
{
59+
throw new ArgumentOutOfRangeException(nameof(period), $"{nameof(period)} should be longer than zero.");
60+
}
61+
3362
_outputTemplate = outputTemplate;
3463
_appName = appName;
64+
_instanceId = instanceId;
3565
_fileName = fileName;
3666
_batchSize = batchSize;
67+
_backgroundQueueSize = backgroundQueueSize;
3768
_period = period;
3869
}
3970

4071
/// <inheritdoc />
41-
public override Logger ConfigureLogger(IWebAppLogConfigurationReader reader)
72+
public Logger ConfigureLogger(IWebAppLogConfigurationReader reader)
4273
{
4374
var messageFormatter = new MessageTemplateTextFormatter(_outputTemplate, null);
4475
var container = new CloudBlobContainer(new Uri(reader.Current.BlobContainerUrl));
45-
var azureBlobSink = new AzureBlobSink(container, _appName, _fileName, messageFormatter, _batchSize, _period);
46-
var backgroundSink = new BackgroundSink(azureBlobSink, BackgroundSink.DefaultLogMessagesQueueSize);
47-
LoggerConfiguration loggerConfiguration = new LoggerConfiguration();
76+
var fileName = _instanceId + "-" + _fileName;
77+
var azureBlobSink = new AzureBlobSink(container, _appName, fileName, messageFormatter, _batchSize, _period);
78+
var backgroundSink = new BackgroundSink(azureBlobSink, _backgroundQueueSize);
79+
var loggerConfiguration = new LoggerConfiguration();
4880

4981
loggerConfiguration.WriteTo.Sink(backgroundSink);
5082
loggerConfiguration.MinimumLevel.ControlledBy(new WebConfigurationReaderLevelSwitch(reader,
5183
configuration =>
5284
{
53-
return configuration.BlobLoggingEnabled ? configuration.BlobLoggingLevel: LogLevel.None;
85+
return configuration.BlobLoggingEnabled ? configuration.BlobLoggingLevel : LogLevel.None;
5486
}));
5587

5688
return loggerConfiguration.CreateLogger();

src/Microsoft.Extensions.Logging.AzureWebAppDiagnostics/Internal/AzureBlobSink.cs

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,30 @@ public AzureBlobSink(CloudBlobContainer container,
4343
int batchSizeLimit,
4444
TimeSpan period) : base(batchSizeLimit, period)
4545
{
46+
if (appName == null)
47+
{
48+
throw new ArgumentNullException(nameof(appName));
49+
}
50+
if (fileName == null)
51+
{
52+
throw new ArgumentNullException(nameof(fileName));
53+
}
54+
if (formatter == null)
55+
{
56+
throw new ArgumentNullException(nameof(formatter));
57+
}
58+
if (batchSizeLimit <= 0)
59+
{
60+
throw new ArgumentOutOfRangeException(nameof(batchSizeLimit), $"{nameof(batchSizeLimit)} should be a positive number.");
61+
}
62+
if (period <= TimeSpan.Zero)
63+
{
64+
throw new ArgumentOutOfRangeException(nameof(period), $"{nameof(period)} should be longer than zero.");
65+
}
66+
4667
_appName = appName;
4768
_fileName = fileName;
4869
_formatter = formatter;
49-
if (batchSizeLimit < 1)
50-
{
51-
throw new ArgumentException(nameof(batchSizeLimit));
52-
}
5370
_container = container;
5471
}
5572

src/Microsoft.Extensions.Logging.AzureWebAppDiagnostics/Internal/FileLoggerProvider.cs

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,13 @@
1111
namespace Microsoft.Extensions.Logging.AzureWebAppDiagnostics.Internal
1212
{
1313
/// <summary>
14-
/// The <see cref="SerilogLoggerProvider"/> implemenation that creates instances of <see cref="Serilog.Core.Logger"/> connected to <see cref="RollingFileSink"/>.
14+
/// The logger provider that creates instances of <see cref="Serilog.Core.Logger"/>.
1515
/// </summary>
16-
public class FileLoggerProvider: SerilogLoggerProvider
16+
public class FileLoggerProvider
1717
{
1818
private readonly int _fileSizeLimit;
1919
private readonly int _retainedFileCountLimit;
20+
private readonly int _backgroundQueueSize;
2021
private readonly string _outputTemplate;
2122

2223
private const string FileNamePattern = "diagnostics-{Date}.txt";
@@ -25,17 +26,32 @@ public class FileLoggerProvider: SerilogLoggerProvider
2526
/// Creates a new instance of the <see cref="FileLoggerProvider"/> class.
2627
/// </summary>
2728
/// <param name="fileSizeLimit">A strictly positive value representing the maximum log size in megabytes. Once the log is full, no more message will be appended</param>
28-
/// <param name="retainedFileCountLimit"></param>
29-
/// <param name="outputTemplate"></param>
30-
public FileLoggerProvider(int fileSizeLimit, int retainedFileCountLimit, string outputTemplate)
29+
/// <param name="retainedFileCountLimit">A strictly positive value representing the maximum retained file count</param>
30+
/// <param name="backgroundQueueSize">The maximum size of the background queue</param>
31+
/// <param name="outputTemplate">A message template describing the output messages</param>
32+
public FileLoggerProvider(int fileSizeLimit, int retainedFileCountLimit, int backgroundQueueSize, string outputTemplate)
3133
{
34+
if (outputTemplate == null)
35+
{
36+
throw new ArgumentNullException(nameof(outputTemplate));
37+
}
38+
if (fileSizeLimit <= 0)
39+
{
40+
throw new ArgumentOutOfRangeException(nameof(fileSizeLimit), $"{nameof(fileSizeLimit)} should be positive.");
41+
}
42+
if (retainedFileCountLimit <= 0)
43+
{
44+
throw new ArgumentOutOfRangeException(nameof(retainedFileCountLimit), $"{nameof(retainedFileCountLimit)} should be positive.");
45+
}
46+
3247
_fileSizeLimit = fileSizeLimit;
3348
_retainedFileCountLimit = retainedFileCountLimit;
49+
_backgroundQueueSize = backgroundQueueSize;
3450
_outputTemplate = outputTemplate;
3551
}
3652

3753
/// <inheritdoc />
38-
public override Logger ConfigureLogger(IWebAppLogConfigurationReader reader)
54+
public Logger ConfigureLogger(IWebAppLogConfigurationReader reader)
3955
{
4056
var webAppConfiguration = reader.Current;
4157
if (string.IsNullOrEmpty(webAppConfiguration.FileLoggingFolder))
@@ -53,9 +69,9 @@ public override Logger ConfigureLogger(IWebAppLogConfigurationReader reader)
5369

5470
var messageFormatter = new MessageTemplateTextFormatter(_outputTemplate, null);
5571
var rollingFileSink = new RollingFileSink(logsFilePattern, messageFormatter, _fileSizeLimit, _retainedFileCountLimit);
56-
var backgroundSink = new BackgroundSink(rollingFileSink, BackgroundSink.DefaultLogMessagesQueueSize);
72+
var backgroundSink = new BackgroundSink(rollingFileSink, _backgroundQueueSize);
5773

58-
LoggerConfiguration loggerConfiguration = new LoggerConfiguration();
74+
var loggerConfiguration = new LoggerConfiguration();
5975
loggerConfiguration.WriteTo.Sink(backgroundSink);
6076
loggerConfiguration.MinimumLevel.ControlledBy(new WebConfigurationReaderLevelSwitch(reader,
6177
configuration =>

src/Microsoft.Extensions.Logging.AzureWebAppDiagnostics/Internal/IWebAppContext.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ public interface IWebAppContext
1818
/// </summary>
1919
string SiteName { get; }
2020

21+
/// <summary>
22+
/// Gets the id of site if running in Azure WebApp
23+
/// </summary>
24+
string SiteInstanceId { get; }
25+
2126
/// <summary>
2227
/// Gets a value indicating whether or new we're in an Azure WebApp
2328
/// </summary>

src/Microsoft.Extensions.Logging.AzureWebAppDiagnostics/Internal/SerilogLoggerProvider.cs

Lines changed: 0 additions & 22 deletions
This file was deleted.

src/Microsoft.Extensions.Logging.AzureWebAppDiagnostics/Internal/WebAppContext.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ private WebAppContext() { }
2323
/// <inheritdoc />
2424
public string SiteName { get; } = Environment.GetEnvironmentVariable("WEBSITE_SITE_NAME");
2525

26+
/// <inheritdoc />
27+
public string SiteInstanceId { get; } = Environment.GetEnvironmentVariable("WEBSITE_INSTANCE_ID");
28+
2629
/// <inheritdoc />
2730
public bool IsRunningInAzureWebApp => !string.IsNullOrEmpty(HomeFolder) &&
2831
!string.IsNullOrEmpty(SiteName);

src/Microsoft.Extensions.Logging.AzureWebAppDiagnostics/Internal/WebConfigurationReaderLevelSwitch.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
34
using System;
45
using Serilog.Core;
56
using Serilog.Events;

test/Microsoft.Extensions.Logging.AzureWebAppDiagnostics.Test/SerilogLoggerProviderTests.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ public void LevelMapping()
135135
testSink.Setup(m => m.Emit(It.IsAny<LogEvent>()));
136136

137137
var provider = new TestWebAppSerilogLoggerProvider(testSink.Object);
138-
var levelsToCheck = new LogLevel[]
138+
var levelsToCheck = new []
139139
{
140140
LogLevel.None,
141141
LogLevel.Critical,
@@ -151,7 +151,7 @@ public void LevelMapping()
151151
loggerFactory.AddSerilog(seriloglogger);
152152
var logger = loggerFactory.CreateLogger("TestLogger");
153153

154-
for (int i = 0; i < levelsToCheck.Length; i++)
154+
for (var i = 0; i < levelsToCheck.Length; i++)
155155
{
156156
var enabledLevel = levelsToCheck[i];
157157

@@ -161,7 +161,7 @@ public void LevelMapping()
161161
configReader.Raise(m => m.OnConfigurationChanged += (sender, e) => { }, null, currentConfig);
162162

163163
// Don't try to log "None" (start at 1)
164-
for (int j = 1; j < levelsToCheck.Length; j++)
164+
for (var j = 1; j < levelsToCheck.Length; j++)
165165
{
166166
logger.Log(levelsToCheck[j], 1, new object(), null, (state, ex) => string.Empty);
167167
}
@@ -177,7 +177,7 @@ public void LevelMapping()
177177
}
178178

179179

180-
private class TestWebAppSerilogLoggerProvider : SerilogLoggerProvider
180+
private class TestWebAppSerilogLoggerProvider
181181
{
182182
private readonly ILogEventSink _sink;
183183

@@ -186,7 +186,7 @@ public TestWebAppSerilogLoggerProvider(ILogEventSink sink)
186186
_sink = sink;
187187
}
188188

189-
public override Logger ConfigureLogger(IWebAppLogConfigurationReader reader)
189+
public Logger ConfigureLogger(IWebAppLogConfigurationReader reader)
190190
{
191191
var loggerConfiguration = new LoggerConfiguration();
192192
loggerConfiguration.WriteTo.Sink(_sink);

0 commit comments

Comments
 (0)