Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions build/NuSpecs/UmbracoCms.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
<!-- config transforms -->
<!-- beware! config transforms not supported by PackageReference -->
<file src="tools\Web.config.install.xdt" target="Content\Web.config.install.xdt" />
<file src="tools\serilog.config.install.xdt" target="Content\config\serilog.config.install.xdt" />
<file src="tools\ClientDependency.config.install.xdt" target="Content\config\ClientDependency.config.install.xdt" />
<file src="tools\umbracoSettings.config.install.xdt" target="Content\config\umbracoSettings.config.install.xdt" />
<file src="tools\Views.Web.config.install.xdt" target="Views\Web.config.install.xdt" /> <!-- FIXME: Content\ !! and then... transform?! -->
Expand Down
9 changes: 9 additions & 0 deletions build/NuSpecs/tools/serilog.config.install.xdt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">>
<appSettings>

<add key="serilog:using:File" value="Umbraco.Core" xdt:Transform="SetAttributes(value)" xdt:Locator="Match(key)"/>
<add key="serilog:write-to:File.shared" xdt:Transform="Remove" xdt:Locator="Match(key)"/>

</appSettings>
</configuration>
55 changes: 48 additions & 7 deletions src/Umbraco.Core/Logging/Serilog/LoggerConfigExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
using System;
using System.Text;
using System.Web;
using Serilog;
using Serilog.Configuration;
using Serilog.Core;
using Serilog.Events;
using Serilog.Formatting;
using Serilog.Formatting.Compact;
using Umbraco.Core.Logging.Serilog.Enrichers;

namespace Umbraco.Core.Logging.Serilog
{
public static class LoggerConfigExtensions
{
private const string AppDomainId = "AppDomainId";
/// <summary>
/// This configures Serilog with some defaults
/// Such as adding ProcessID, Thread, AppDomain etc
Expand All @@ -28,14 +33,14 @@ public static LoggerConfiguration MinimalConfiguration(this LoggerConfiguration
.Enrich.WithProcessId()
.Enrich.WithProcessName()
.Enrich.WithThreadId()
.Enrich.WithProperty("AppDomainId", AppDomain.CurrentDomain.Id)
.Enrich.WithProperty(AppDomainId, AppDomain.CurrentDomain.Id)
.Enrich.WithProperty("AppDomainAppId", HttpRuntime.AppDomainAppId.ReplaceNonAlphanumericChars(string.Empty))
.Enrich.WithProperty("MachineName", Environment.MachineName)
.Enrich.With<Log4NetLevelMapperEnricher>()
.Enrich.With<HttpSessionIdEnricher>()
.Enrich.With<HttpRequestNumberEnricher>()
.Enrich.With<HttpRequestIdEnricher>();

return logConfig;
}

Expand All @@ -50,15 +55,51 @@ public static LoggerConfiguration OutputDefaultTextFile(this LoggerConfiguration
//Main .txt logfile - in similar format to older Log4Net output
//Ends with ..txt as Date is inserted before file extension substring
logConfig.WriteTo.File($@"{AppDomain.CurrentDomain.BaseDirectory}\App_Data\Logs\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}");
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.
/// </remarks>
public static LoggerConfiguration File(this LoggerSinkConfiguration configuration, ITextFormatter formatter,
string path,
LogEventLevel restrictedToMinimumLevel = LogEventLevel.Verbose,
LoggingLevelSwitch levelSwitch = null,
long? fileSizeLimitBytes = 1073741824,
TimeSpan? flushToDiskInterval = null,
RollingInterval rollingInterval = RollingInterval.Infinite,
bool rollOnFileSizeLimit = false,
int? retainedFileCountLimit = 31,
Encoding encoding = null
)
{
return configuration.Async(
asyncConfiguration => asyncConfiguration.Map(AppDomainId, (_,mapConfiguration) =>
mapConfiguration.File(
formatter,
path,
restrictedToMinimumLevel,
fileSizeLimitBytes,
levelSwitch,
buffered:true,
shared:false,
flushToDiskInterval,
rollingInterval,
rollOnFileSizeLimit,
retainedFileCountLimit,
encoding),
sinkMapCountLimit:0)
);
}


/// <summary>
/// Outputs a CLEF format JSON log at /App_Data/Logs/
/// </summary>
Expand Down
7 changes: 6 additions & 1 deletion src/Umbraco.Core/Logging/Serilog/SerilogLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace Umbraco.Core.Logging.Serilog
///<summary>
/// Implements <see cref="ILogger"/> on top of Serilog.
///</summary>
public class SerilogLogger : ILogger
public class SerilogLogger : ILogger, IDisposable
{
/// <summary>
/// Initialize a new instance of the <see cref="SerilogLogger"/> class with a configuration file.
Expand Down Expand Up @@ -271,5 +271,10 @@ public void Verbose(Type reporting, string messageTemplate, params object[] prop
{
LoggerFor(reporting).Verbose(messageTemplate, propertyValues);
}

public void Dispose()
{
Log.CloseAndFlush();
}
}
}
9 changes: 7 additions & 2 deletions src/Umbraco.Core/Logging/Viewer/JsonLogViewer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public override bool CheckCanOpenLogs(DateTimeOffset startDate, DateTimeOffset e
for (var day = startDate.Date; day.Date <= endDate.Date; day = day.AddDays(1))
{
//Filename ending to search for (As could be multiple)
var filesToFind = $"*{day:yyyyMMdd}.json";
var filesToFind = GetSearchPattern(day);

var filesForCurrentDay = Directory.GetFiles(logDirectory, filesToFind);

Expand All @@ -52,6 +52,11 @@ public override bool CheckCanOpenLogs(DateTimeOffset startDate, DateTimeOffset e
return logSizeAsMegabytes <= FileSizeCap;
}

private string GetSearchPattern(DateTime day)
{
return $"*{day:yyyyMMdd}*.json";
}

protected override IReadOnlyList<LogEvent> GetLogs(DateTimeOffset startDate, DateTimeOffset endDate, ILogFilter filter, int skip, int take)
{
var logs = new List<LogEvent>();
Expand All @@ -66,7 +71,7 @@ protected override IReadOnlyList<LogEvent> GetLogs(DateTimeOffset startDate, Dat
for (var day = startDate.Date; day.Date <= endDate.Date; day = day.AddDays(1))
{
//Filename ending to search for (As could be multiple)
var filesToFind = $"*{day:yyyyMMdd}.json";
var filesToFind = GetSearchPattern(day);

var filesForCurrentDay = Directory.GetFiles(logDirectory, filesToFind);

Expand Down
6 changes: 6 additions & 0 deletions src/Umbraco.Core/Umbraco.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@
</ItemGroup>
<ItemGroup>
<!-- note: NuGet deals with transitive references now -->
<PackageReference Include="Serilog.Sinks.Async">
<Version>1.3.0</Version>
</PackageReference>
<PackageReference Include="Serilog.Sinks.Map">
<Version>1.0.0</Version>
</PackageReference>
<PackageReference Include="Umbraco.Code">
<Version>1.0.5</Version>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
Expand Down
4 changes: 2 additions & 2 deletions src/Umbraco.Web.UI/config/serilog.Release.config
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@

<!-- Default JSON log file -->
<!-- This is used by the default log viewer in the Umbraco backoffice -->
<add key="serilog:using:File" value="Serilog.Sinks.File" />
<add key="serilog:using:File" value="Umbraco.Core" />

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bergmania With these config changes we're going to need a xdt transform so that both Cloud and Nuget upgrades get the changes auto applied. I'll get one made.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bergmania don't we also want to apply these changes to the non-release config file?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There don't exists a non-release one. That file is copied from the release one in a before build step in Umbraco.Web.UI.cspoj. I don't know the reason for this.

<!-- Create Serilog.config & serilog.user.config file from Templates if it doesn't exist -->
    <Message Text="Copy serilog.$(Configuration).config to serilog.config" Importance="high" Condition="!Exists('$(ProjectDir)Config\serilog.config')" />
    <Copy SourceFiles="$(ProjectDir)Config\serilog.Release.config" DestinationFiles="$(ProjectDir)Config\serilog.config" OverwriteReadOnlyFiles="true" SkipUnchangedFiles="false" Condition="!Exists('$(ProjectDir)Config\serilog.config')" />
    <Message Text="Copy serilog.user.$(Configuration).config to serilog.user.config" Importance="high" Condition="!Exists('$(ProjectDir)Config\serilog.user.config')" />
    <Copy SourceFiles="$(ProjectDir)Config\serilog.user.Release.config" DestinationFiles="$(ProjectDir)Config\serilog.user.config" OverwriteReadOnlyFiles="true" SkipUnchangedFiles="false" Condition="!Exists('$(ProjectDir)Config\serilog.user.config')" />
    

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah of course 🤦‍♂ it's just for developer testing, that's the only reason it's not checked in, not sure when/why that decision was made but that's how it is i guess since that will be the same for some other config files.

<add key="serilog:write-to:File.formatter" value="Serilog.Formatting.Compact.CompactJsonFormatter, Serilog.Formatting.Compact" />
<add key="serilog:write-to:File.path" value="%BASEDIR%\App_Data\Logs\UmbracoTraceLog.%MACHINENAME%..json" />
<add key="serilog:write-to:File.shared" value="true" />
<add key="serilog:write-to:File.restrictedToMinimumLevel" value="Debug" />
<add key="serilog:write-to:File.retainedFileCountLimit" value="" /> <!-- Number of log files to keep (or remove value to keep all files) -->
<add key="serilog:write-to:File.rollingInterval" value="Day" /> <!-- Create a new log file every Minute/Hour/Day/Month/Year/infinite -->


<!-- Optional TXT log file -->
<!--<add key="serilog:using:File" value="Serilog.Sinks.File" /> -->
<!--<add key="serilog:write-to:File.path" value="%BASEDIR%\App_Data\Logs\UmbracoTraceLog.%MACHINENAME%..txt" /> -->
Expand Down
2 changes: 2 additions & 0 deletions src/Umbraco.Web/UmbracoApplicationBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Umbraco.Core;
using Umbraco.Core.Composing;
using Umbraco.Core.Logging;
using Umbraco.Core.Logging.Serilog;

namespace Umbraco.Web
{
Expand Down Expand Up @@ -143,6 +144,7 @@ internal void HandleApplicationEnd()
Current.Logger.Info<UmbracoApplicationBase>("Application shutdown. Reason: {ShutdownReason}", HostingEnvironment.ShutdownReason);
}

Current.Logger.DisposeIfDisposable();
// dispose the container and everything
Current.Reset();
}
Expand Down
2 changes: 1 addition & 1 deletion src/umbraco.sln
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tools", "tools", "{E3F9F378
..\build\NuSpecs\tools\install.ps1 = ..\build\NuSpecs\tools\install.ps1
..\build\NuSpecs\tools\Readme.txt = ..\build\NuSpecs\tools\Readme.txt
..\build\NuSpecs\tools\ReadmeUpgrade.txt = ..\build\NuSpecs\tools\ReadmeUpgrade.txt
..\build\NuSpecs\tools\trees.config.install.xdt = ..\build\NuSpecs\tools\trees.config.install.xdt
..\build\NuSpecs\tools\serilog.config.install.xdt = ..\build\NuSpecs\tools\serilog.config.install.xdt
..\build\NuSpecs\tools\Web.config.install.xdt = ..\build\NuSpecs\tools\Web.config.install.xdt
EndProjectSection
EndProject
Expand Down