diff --git a/README.md b/README.md index d84adbc..347a228 100644 --- a/README.md +++ b/README.md @@ -26,4 +26,19 @@ And finally utilise the token in your logging template: var template = "{MemoryUsage}"; ``` +You can also enrich your logging configuration for virtual memory: + +```csharp +new LoggerConfiguration() + .Enrich.WithVirtualMemoryUsage(); +``` + +And finally utilise the token in your logging template: + +```csharp +var template = "{VirtualMemoryUsage}"; +``` + +Please notice VirtualMemoryUsage is not available for .NET Standard 1.3 due to Process API unavailability. + Copyright © 2017 Josh Schreuder - Provided under the [Apache License, Version 2.0](http://apache.org/licenses/LICENSE-2.0.html). diff --git a/src/Serilog.Enrichers.Memory/Enrichers/VirtualMemoryUsageEnricher.cs b/src/Serilog.Enrichers.Memory/Enrichers/VirtualMemoryUsageEnricher.cs new file mode 100644 index 0000000..6d2d18b --- /dev/null +++ b/src/Serilog.Enrichers.Memory/Enrichers/VirtualMemoryUsageEnricher.cs @@ -0,0 +1,50 @@ +// Copyright 2017 Josh Schreuder +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +using System.Diagnostics; +using Serilog.Core; +using Serilog.Events; + +namespace Serilog.Enrichers +{ + /// + /// Enriches log events with a MemoryUsage property containing an estimation of the current process' virtual memory usage in bytes. + /// + public class VirtualMemoryUsageEnricher : ILogEventEnricher + { + /// + /// The property name added to enriched log events. + /// + public const string MemoryUsagePropertyName = "VirtualMemoryUsage"; + + /// + /// Enrich the log event. + /// + /// The log event to enrich. + /// Factory for creating new properties to add to the event. + public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory) + { + logEvent.AddPropertyIfAbsent(propertyFactory.CreateProperty(MemoryUsagePropertyName, GetProcessVirtualMemoryUsage())); + } + + private long GetProcessVirtualMemoryUsage() + { +#if NETSTANDARD1_3 + return -1; +#else + return Process.GetCurrentProcess().VirtualMemorySize64; +#endif // NETSTANDARD1_3 + } + } +} \ No newline at end of file diff --git a/src/Serilog.Enrichers.Memory/ProcessLoggerConfigurationExtensions.cs b/src/Serilog.Enrichers.Memory/ProcessLoggerConfigurationExtensions.cs index 73525ee..3a258cf 100644 --- a/src/Serilog.Enrichers.Memory/ProcessLoggerConfigurationExtensions.cs +++ b/src/Serilog.Enrichers.Memory/ProcessLoggerConfigurationExtensions.cs @@ -36,5 +36,17 @@ public static LoggerConfiguration WithMemoryUsage( if (enrichmentConfiguration == null) throw new ArgumentNullException(nameof(enrichmentConfiguration)); return enrichmentConfiguration.With(); } + + /// + /// Enrich log events with virtual memory usage/>. + /// + /// Logger enrichment configuration. + /// Configuration object allowing method chaining. + public static LoggerConfiguration WithVirtualMemoryUsage( + this LoggerEnrichmentConfiguration enrichmentConfiguration) + { + if (enrichmentConfiguration == null) throw new ArgumentNullException(nameof(enrichmentConfiguration)); + return enrichmentConfiguration.With(); + } } } \ No newline at end of file