Skip to content

Commit a6903d8

Browse files
authored
V9: Display configured log levels (#11715)
* Adding ILogLevelLoader to get Serilog level values (global one and umbraco) and the corresponding dependencies * Obsoleting old methods * Fix tests * Front-end changes * Changes requested in a review
1 parent 568dc19 commit a6903d8

File tree

14 files changed

+145
-24
lines changed

14 files changed

+145
-24
lines changed

src/Umbraco.Infrastructure/DependencyInjection/UmbracoBuilder.CoreServices.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,10 +268,12 @@ private static IUmbracoBuilder AddPreValueMigrators(this IUmbracoBuilder builder
268268
public static IUmbracoBuilder AddLogViewer(this IUmbracoBuilder builder)
269269
{
270270
builder.Services.AddSingleton<ILogViewerConfig, LogViewerConfig>();
271+
builder.Services.AddSingleton<ILogLevelLoader, LogLevelLoader>();
271272
builder.SetLogViewer<SerilogJsonLogViewer>();
272273
builder.Services.AddSingleton<ILogViewer>(factory => new SerilogJsonLogViewer(factory.GetRequiredService<ILogger<SerilogJsonLogViewer>>(),
273274
factory.GetRequiredService<ILogViewerConfig>(),
274275
factory.GetRequiredService<ILoggingConfiguration>(),
276+
factory.GetRequiredService<ILogLevelLoader>(),
275277
Log.Logger));
276278

277279
return builder;

src/Umbraco.Infrastructure/Logging/Serilog/LoggerConfigExtensions.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ public static LoggerConfiguration MinimalConfiguration(
3131
this LoggerConfiguration logConfig,
3232
IHostingEnvironment hostingEnvironment,
3333
ILoggingConfiguration loggingConfiguration,
34-
IConfiguration configuration)
34+
IConfiguration configuration,
35+
out UmbracoFileConfiguration umbFileConfiguration)
3536
{
3637
global::Serilog.Debugging.SelfLog.Enable(msg => System.Diagnostics.Debug.WriteLine(msg));
3738

@@ -54,6 +55,8 @@ public static LoggerConfiguration MinimalConfiguration(
5455
//This is not optimal, but seems to be the only way if we do not make an Serilog.Sink.UmbracoFile sink all the way.
5556
var umbracoFileConfiguration = new UmbracoFileConfiguration(configuration);
5657

58+
umbFileConfiguration = umbracoFileConfiguration;
59+
5760
logConfig.WriteTo.UmbracoFile(
5861
path : umbracoFileConfiguration.GetPath(loggingConfiguration.LogDirectory),
5962
fileSizeLimitBytes: umbracoFileConfiguration.FileSizeLimitBytes,

src/Umbraco.Infrastructure/Logging/Serilog/SerilogLogger.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using Serilog;
44
using Serilog.Events;
55
using Umbraco.Cms.Core.Hosting;
6+
using Umbraco.Cms.Infrastructure.Logging.Serilog;
67
using Umbraco.Extensions;
78

89
namespace Umbraco.Cms.Core.Logging.Serilog
@@ -27,13 +28,14 @@ public SerilogLogger(LoggerConfiguration logConfig)
2728
public static SerilogLogger CreateWithDefaultConfiguration(
2829
IHostingEnvironment hostingEnvironment,
2930
ILoggingConfiguration loggingConfiguration,
30-
IConfiguration configuration)
31+
IConfiguration configuration,
32+
out UmbracoFileConfiguration umbracoFileConfig)
3133
{
32-
var loggerConfig = new LoggerConfiguration()
33-
.MinimalConfiguration(hostingEnvironment, loggingConfiguration, configuration)
34+
var serilogConfig = new LoggerConfiguration()
35+
.MinimalConfiguration(hostingEnvironment, loggingConfiguration, configuration, out umbracoFileConfig)
3436
.ReadFrom.Configuration(configuration);
3537

36-
return new SerilogLogger(loggerConfig);
38+
return new SerilogLogger(serilogConfig);
3739
}
3840

3941
/// <summary>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using System.Collections.ObjectModel;
2+
using Serilog.Events;
3+
4+
namespace Umbraco.Cms.Core.Logging.Viewer
5+
{
6+
public interface ILogLevelLoader
7+
{
8+
/// <summary>
9+
/// Get the Serilog level values of the global minimum and the UmbracoFile one from the config file.
10+
/// </summary>
11+
ReadOnlyDictionary<string, LogEventLevel> GetLogLevelsFromSinks();
12+
13+
/// <summary>
14+
/// Get the Serilog minimum-level value from the config file.
15+
/// </summary>
16+
LogEventLevel GetGlobalMinLogLevel();
17+
}
18+
}

src/Umbraco.Infrastructure/Logging/Viewer/ILogViewer.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
using System.Collections.Generic;
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Collections.ObjectModel;
4+
using Serilog.Events;
25
using Umbraco.Cms.Core.Models;
36

47
namespace Umbraco.Cms.Core.Logging.Viewer
@@ -40,10 +43,16 @@ public interface ILogViewer
4043

4144
bool CheckCanOpenLogs(LogTimePeriod logTimePeriod);
4245

46+
/// <summary>
47+
/// Get the Serilog minimum-level and UmbracoFile-level values from the config file.
48+
/// </summary>
49+
ReadOnlyDictionary<string, LogEventLevel> GetLogLevels();
50+
4351
/// <summary>
4452
/// Gets the current Serilog minimum log level
4553
/// </summary>
4654
/// <returns></returns>
55+
[Obsolete("Please use GetLogLevels() instead. Scheduled for removal in V11.")]
4756
string GetLogLevel();
4857

4958
/// <summary>
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Collections.ObjectModel;
4+
using System.Linq;
5+
using System.Text;
6+
using Serilog;
7+
using Serilog.Events;
8+
using Umbraco.Cms.Infrastructure.Logging.Serilog;
9+
10+
namespace Umbraco.Cms.Core.Logging.Viewer
11+
{
12+
public class LogLevelLoader : ILogLevelLoader
13+
{
14+
private readonly UmbracoFileConfiguration _umbracoFileConfig;
15+
16+
public LogLevelLoader(UmbracoFileConfiguration umbracoFileConfig) => _umbracoFileConfig = umbracoFileConfig;
17+
18+
/// <summary>
19+
/// Get the Serilog level values of the global minimum and the UmbracoFile one from the config file.
20+
/// </summary>
21+
public ReadOnlyDictionary<string, LogEventLevel> GetLogLevelsFromSinks()
22+
{
23+
var configuredLogLevels = new Dictionary<string, LogEventLevel>
24+
{
25+
{ "Global", GetGlobalMinLogLevel() },
26+
{ "UmbracoFile", _umbracoFileConfig.RestrictedToMinimumLevel }
27+
};
28+
29+
return new ReadOnlyDictionary<string, LogEventLevel>(configuredLogLevels);
30+
}
31+
32+
/// <summary>
33+
/// Get the Serilog minimum-level value from the config file.
34+
/// </summary>
35+
public LogEventLevel GetGlobalMinLogLevel()
36+
{
37+
var logLevel = Enum.GetValues(typeof(LogEventLevel)).Cast<LogEventLevel>().Where(Log.IsEnabled).DefaultIfEmpty(LogEventLevel.Information)?.Min() ?? null;
38+
return (LogEventLevel)logLevel;
39+
}
40+
}
41+
}

src/Umbraco.Infrastructure/Logging/Viewer/SerilogJsonLogViewer.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
using System;
1+
using System;
22
using System.Collections.Generic;
33
using System.IO;
44
using System.Linq;
55
using Microsoft.Extensions.Logging;
66
using Newtonsoft.Json;
77
using Serilog.Events;
88
using Serilog.Formatting.Compact.Reader;
9-
using Umbraco.Cms.Core.Logging;
109

1110
namespace Umbraco.Cms.Core.Logging.Viewer
1211
{
@@ -19,8 +18,9 @@ public SerilogJsonLogViewer(
1918
ILogger<SerilogJsonLogViewer> logger,
2019
ILogViewerConfig logViewerConfig,
2120
ILoggingConfiguration loggingConfiguration,
21+
ILogLevelLoader logLevelLoader,
2222
global::Serilog.ILogger serilogLog)
23-
: base(logViewerConfig, serilogLog)
23+
: base(logViewerConfig, logLevelLoader, serilogLog)
2424
{
2525
_logger = logger;
2626
_logsPath = loggingConfiguration.LogDirectory;

src/Umbraco.Infrastructure/Logging/Viewer/SerilogLogViewerSourceBase.cs

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,33 @@
1-
using System;
1+
using System;
22
using System.Collections.Generic;
3+
using System.Collections.ObjectModel;
34
using System.Linq;
5+
using Microsoft.Extensions.DependencyInjection;
46
using Serilog.Events;
57
using Umbraco.Cms.Core.Models;
8+
using Umbraco.Cms.Web.Common.DependencyInjection;
69
using Umbraco.Extensions;
710

811
namespace Umbraco.Cms.Core.Logging.Viewer
912
{
1013
public abstract class SerilogLogViewerSourceBase : ILogViewer
1114
{
1215
private readonly ILogViewerConfig _logViewerConfig;
16+
private readonly ILogLevelLoader _logLevelLoader;
1317
private readonly global::Serilog.ILogger _serilogLog;
1418

19+
[Obsolete("Please use ctor with all params instead. Scheduled for removal in V11.")]
1520
protected SerilogLogViewerSourceBase(ILogViewerConfig logViewerConfig, global::Serilog.ILogger serilogLog)
1621
{
1722
_logViewerConfig = logViewerConfig;
23+
_logLevelLoader = StaticServiceProvider.Instance.GetRequiredService<ILogLevelLoader>();
24+
_serilogLog = serilogLog;
25+
}
26+
27+
protected SerilogLogViewerSourceBase(ILogViewerConfig logViewerConfig, ILogLevelLoader logLevelLoader, global::Serilog.ILogger serilogLog)
28+
{
29+
_logViewerConfig = logViewerConfig;
30+
_logLevelLoader = logLevelLoader;
1831
_serilogLog = serilogLog;
1932
}
2033

@@ -43,14 +56,22 @@ public int GetNumberOfErrors(LogTimePeriod logTimePeriod)
4356
return errorCounter.Count;
4457
}
4558

59+
/// <summary>
60+
/// Get the Serilog minimum-level and UmbracoFile-level values from the config file.
61+
/// </summary>
62+
public ReadOnlyDictionary<string, LogEventLevel> GetLogLevels()
63+
{
64+
return _logLevelLoader.GetLogLevelsFromSinks();
65+
}
66+
4667
/// <summary>
4768
/// Get the Serilog minimum-level value from the config file.
4869
/// </summary>
49-
/// <returns></returns>
70+
[Obsolete("Please use LogLevelLoader.GetGlobalMinLogLevel() instead. Scheduled for removal in V11.")]
5071
public string GetLogLevel()
5172
{
5273
var logLevel = Enum.GetValues(typeof(LogEventLevel)).Cast<LogEventLevel>().Where(_serilogLog.IsEnabled).DefaultIfEmpty(LogEventLevel.Information)?.Min() ?? null;
53-
return logLevel?.ToString() ?? "";
74+
return logLevel?.ToString() ?? string.Empty;
5475
}
5576

5677
public LogLevelCounts GetLogLevelCounts(LogTimePeriod logTimePeriod)
@@ -129,7 +150,5 @@ public PagedResult<LogMessage> GetLogs(LogTimePeriod logTimePeriod,
129150
Items = logMessages
130151
};
131152
}
132-
133-
134153
}
135154
}

src/Umbraco.Web.BackOffice/Controllers/LogViewerController.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Collections.ObjectModel;
34
using Microsoft.AspNetCore.Authorization;
45
using Microsoft.AspNetCore.Mvc;
6+
using Serilog.Events;
57
using Umbraco.Cms.Core;
68
using Umbraco.Cms.Core.Logging.Viewer;
79
using Umbraco.Cms.Core.Models;
@@ -135,6 +137,13 @@ public IEnumerable<SavedLogSearch> DeleteSavedSearch(SavedLogSearch item)
135137
return _logViewer.DeleteSavedSearch(item.Name, item.Query);
136138
}
137139

140+
[HttpGet]
141+
public ReadOnlyDictionary<string, LogEventLevel> GetLogLevels()
142+
{
143+
return _logViewer.GetLogLevels();
144+
}
145+
146+
[Obsolete("Please use GetLogLevels() instead. Scheduled for removal in V11.")]
138147
[HttpGet]
139148
public string GetLogLevel()
140149
{

src/Umbraco.Web.Common/Extensions/ServiceCollectionExtensions.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System;
12
using System.IO;
23
using System.Reflection;
34
using Microsoft.AspNetCore.Hosting;
@@ -29,7 +30,8 @@ public static IServiceCollection AddLogger(
2930
IConfiguration configuration)
3031
{
3132
// Create a serilog logger
32-
var logger = SerilogLogger.CreateWithDefaultConfiguration(hostingEnvironment, loggingConfiguration, configuration);
33+
var logger = SerilogLogger.CreateWithDefaultConfiguration(hostingEnvironment, loggingConfiguration, configuration, out var umbracoFileConfig);
34+
services.AddSingleton(umbracoFileConfig);
3335

3436
// This is nessasary to pick up all the loggins to MS ILogger.
3537
Log.Logger = logger.SerilogLog;

0 commit comments

Comments
 (0)