Skip to content

Commit

Permalink
V11: Allow changing logging directory from configuration (#13485)
Browse files Browse the repository at this point in the history
* Allow changing logging directory from configuration

* Clean up

* Missing return statement

* Apply suggestions from code review

Co-authored-by: Nikolaj Geisle <[email protected]>

* Update src/Umbraco.Core/Constants-SystemDirectories.cs

Co-authored-by: Nikolaj Geisle <[email protected]>

Co-authored-by: Nikolaj Geisle <[email protected]>
  • Loading branch information
bergmania and Zeegaan authored Nov 29, 2022
1 parent 188fa23 commit bfe9579
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 5 deletions.
20 changes: 20 additions & 0 deletions src/Umbraco.Core/Configuration/Models/LoggingSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// See LICENSE for more details.

using System.ComponentModel;
using Microsoft.Extensions.Hosting;
using Umbraco.Cms.Core.Extensions;

namespace Umbraco.Cms.Core.Configuration.Models;

Expand All @@ -12,10 +14,28 @@ namespace Umbraco.Cms.Core.Configuration.Models;
public class LoggingSettings
{
internal const string StaticMaxLogAge = "1.00:00:00"; // TimeSpan.FromHours(24);
internal const string StaticDirectory = Constants.SystemDirectories.LogFiles;

/// <summary>
/// Gets or sets a value for the maximum age of a log file.
/// </summary>
[DefaultValue(StaticMaxLogAge)]
public TimeSpan MaxLogAge { get; set; } = TimeSpan.Parse(StaticMaxLogAge);

/// <summary>
/// Gets or sets the folder to use for log files
/// </summary>
[DefaultValue(StaticDirectory)]
public string Directory { get; set; } = StaticDirectory;

public string GetAbsoluteLoggingPath(IHostEnvironment hostEnvironment)
{
var dir = Directory;
if (dir.StartsWith("~/"))
{
return hostEnvironment.MapPathContentRoot(dir);
}

return dir;
}
}
1 change: 1 addition & 0 deletions src/Umbraco.Core/Constants-SystemDirectories.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ public static class SystemDirectories
/// <summary>
/// The default folder where Umbraco log files are stored
/// </summary>
[Obsolete("Use LoggingSettings.GetLoggingDirectory() instead, will be removed in Umbraco 13.")]
public const string LogFiles = Umbraco + "/Logs";

[Obsolete("Use PluginIcons instead")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Serilog.Events;
using Serilog.Formatting;
using Serilog.Formatting.Compact;
using Umbraco.Cms.Core.Configuration.Models;
using Umbraco.Cms.Core.Extensions;
using Umbraco.Cms.Core.Logging;
using Umbraco.Cms.Core.Logging.Serilog.Enrichers;
Expand All @@ -21,7 +22,7 @@ public static class LoggerConfigExtensions
/// Such as adding ProcessID, Thread, AppDomain etc
/// It is highly recommended that you keep/use this default in your own logging config customizations
/// </summary>
[Obsolete("Please use an alternative method.")]
[Obsolete("Please use an alternative method. This will be removed in Umbraco 13.")]
public static LoggerConfiguration MinimalConfiguration(
this LoggerConfiguration logConfig,
Umbraco.Cms.Core.Hosting.IHostingEnvironment hostingEnvironment,
Expand All @@ -36,7 +37,7 @@ public static LoggerConfiguration MinimalConfiguration(
/// Such as adding ProcessID, Thread, AppDomain etc
/// It is highly recommended that you keep/use this default in your own logging config customizations
/// </summary>
[Obsolete("Please use an alternative method.")]
[Obsolete("Please use an alternative method. This will be removed in Umbraco 13.")]
public static LoggerConfiguration MinimalConfiguration(
this LoggerConfiguration logConfig,
Umbraco.Cms.Core.Hosting.IHostingEnvironment hostingEnvironment,
Expand Down Expand Up @@ -126,6 +127,7 @@ public static LoggerConfiguration MinimalConfiguration(
/// <param name="loggingConfiguration"></param>
/// <param name="minimumLevel">The log level you wish the JSON file to collect - default is Verbose (highest)</param>
/// <param name="retainedFileCount">The number of days to keep log files. Default is set to null which means all logs are kept</param>
[Obsolete("Will be removed in Umbraco 13.")]
public static LoggerConfiguration OutputDefaultTextFile(
this LoggerConfiguration logConfig,
Umbraco.Cms.Core.Hosting.IHostingEnvironment hostingEnvironment,
Expand All @@ -144,6 +146,31 @@ public static LoggerConfiguration OutputDefaultTextFile(
return logConfig;
}

/// <summary>
/// Outputs a .txt format log at /App_Data/Logs/
/// </summary>
/// <param name="logConfig">A Serilog LoggerConfiguration</param>
/// <param name="hostEnvironment"></param>
/// <param name="loggingSettings"></param>
/// <param name="minimumLevel">The log level you wish the JSON file to collect - default is Verbose (highest)</param>
public static LoggerConfiguration OutputDefaultTextFile(
this LoggerConfiguration logConfig,
IHostEnvironment hostEnvironment,
LoggingSettings loggingSettings,
LogEventLevel minimumLevel = LogEventLevel.Verbose)
{
//Main .txt logfile - in similar format to older Log4Net output
//Ends with ..txt as Date is inserted before file extension substring
logConfig.WriteTo.File(
Path.Combine(loggingSettings.GetAbsoluteLoggingPath(hostEnvironment), $"UmbracoTraceLog.{Environment.MachineName}..txt"),
shared: true,
rollingInterval: RollingInterval.Day,
restrictedToMinimumLevel: minimumLevel,
retainedFileCountLimit: null, //Setting to null means we keep all files - default is 31 days
outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss,fff} [P{ProcessId}/D{AppDomainId}/T{ThreadId}] {Log4NetLevel} {SourceContext} - {Message:lj}{NewLine}{Exception}");

return logConfig;
}

/// <remarks>
/// Used in config - If renamed or moved to other assembly the config file also has be updated.
Expand Down Expand Up @@ -195,6 +222,7 @@ public static LoggerConfiguration UmbracoFile(
/// <param name="minimumLevel">The log level you wish the JSON file to collect - default is Verbose (highest)</param>
/// <param name="hostingEnvironment"></param>
/// <param name="retainedFileCount">The number of days to keep log files. Default is set to null which means all logs are kept</param>
[Obsolete("Will be removed in Umbraco 13.")]
public static LoggerConfiguration OutputDefaultJsonFile(
this LoggerConfiguration logConfig,
Umbraco.Cms.Core.Hosting.IHostingEnvironment hostingEnvironment,
Expand All @@ -215,5 +243,33 @@ public static LoggerConfiguration OutputDefaultJsonFile(
return logConfig;
}

/// <summary>
/// Outputs a CLEF format JSON log at /App_Data/Logs/
/// </summary>
/// <param name="logConfig">A Serilog LoggerConfiguration</param>
/// <param name="hostEnvironment"></param>
/// <param name="loggingSettings">The logging configuration</param>
/// <param name="minimumLevel">The log level you wish the JSON file to collect - default is Verbose (highest)</param>
/// <param name="retainedFileCount">The number of days to keep log files. Default is set to null which means all logs are kept</param>
public static LoggerConfiguration OutputDefaultJsonFile(
this LoggerConfiguration logConfig,
IHostEnvironment hostEnvironment,
LoggingSettings loggingSettings,
LogEventLevel minimumLevel = LogEventLevel.Verbose,
int? retainedFileCount = null)
{
// .clef format (Compact log event format, that can be imported into local SEQ & will make searching/filtering logs easier)
// Ends with ..txt as Date is inserted before file extension substring
logConfig.WriteTo.File(
new CompactJsonFormatter(),
Path.Combine(loggingSettings.GetAbsoluteLoggingPath(hostEnvironment) ,$"UmbracoTraceLog.{Environment.MachineName}..json"),
shared: true,
rollingInterval: RollingInterval.Day, // Create a new JSON file every day
retainedFileCountLimit: retainedFileCount, // Setting to null means we keep all files - default is 31 days
restrictedToMinimumLevel: minimumLevel);

return logConfig;
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ private Result HandleInstallException(Exception ex)
{
Message =
"The database configuration failed with the following message: " + ex.Message +
$"\n Please check log file for additional information (can be found in '{Constants.SystemDirectories.LogFiles}')",
$"\n Please check log file for additional information (can be found in '{nameof(LoggingSettings)}.{nameof(LoggingSettings.Directory)}')",
Success = false,
Percentage = "90"
};
Expand Down
12 changes: 10 additions & 2 deletions src/Umbraco.Web.Common/Extensions/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,9 @@ public static IServiceCollection AddLogger(
IHostEnvironment hostEnvironment,
IConfiguration configuration)
{
// TODO: WEBSITE_RUN_FROM_PACKAGE - can't assume this DIR is writable - we have an IConfiguration instance so a later refactor should be easy enough.
var loggingDir = hostEnvironment.MapPathContentRoot(Constants.SystemDirectories.LogFiles);
LoggingSettings loggerSettings = GetLoggerSettings(configuration);

var loggingDir = loggerSettings.GetAbsoluteLoggingPath(hostEnvironment);
ILoggingConfiguration loggingConfig = new LoggingConfiguration(loggingDir);

var umbracoFileConfiguration = new UmbracoFileConfiguration(configuration);
Expand Down Expand Up @@ -146,6 +147,13 @@ public static IServiceCollection AddLogger(
return services;
}

private static LoggingSettings GetLoggerSettings(IConfiguration configuration)
{
var loggerSettings = new LoggingSettings();
configuration.GetSection(Constants.Configuration.ConfigLogging).Bind(loggerSettings);
return loggerSettings;
}

/// <summary>
/// Called to create the <see cref="TypeLoader" /> to assign to the <see cref="IUmbracoBuilder" />
/// </summary>
Expand Down

0 comments on commit bfe9579

Please sign in to comment.