Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ądded support for virtual memory usage #2

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Original file line number Diff line number Diff line change
@@ -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
{
/// <summary>
/// Enriches log events with a MemoryUsage property containing an estimation of the current process' virtual memory usage in bytes.
/// </summary>
public class VirtualMemoryUsageEnricher : ILogEventEnricher
{
/// <summary>
/// The property name added to enriched log events.
/// </summary>
public const string MemoryUsagePropertyName = "VirtualMemoryUsage";

/// <summary>
/// Enrich the log event.
/// </summary>
/// <param name="logEvent">The log event to enrich.</param>
/// <param name="propertyFactory">Factory for creating new properties to add to the event.</param>
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
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,17 @@ public static LoggerConfiguration WithMemoryUsage(
if (enrichmentConfiguration == null) throw new ArgumentNullException(nameof(enrichmentConfiguration));
return enrichmentConfiguration.With<MemoryUsageEnricher>();
}

/// <summary>
/// Enrich log events with virtual memory usage/>.
/// </summary>
/// <param name="enrichmentConfiguration">Logger enrichment configuration.</param>
/// <returns>Configuration object allowing method chaining.</returns>
public static LoggerConfiguration WithVirtualMemoryUsage(
this LoggerEnrichmentConfiguration enrichmentConfiguration)
{
if (enrichmentConfiguration == null) throw new ArgumentNullException(nameof(enrichmentConfiguration));
return enrichmentConfiguration.With<VirtualMemoryUsageEnricher>();
}
}
}