Skip to content

Commit

Permalink
feat: WASM ILoggerProvider MinLogLevel support (#291)
Browse files Browse the repository at this point in the history
Co-authored-by: Tariq Ahmed <hannibalthannibal@outlook.com>
  • Loading branch information
tariqalsoahmed and Tariq Ahmed committed May 26, 2024
1 parent 2543748 commit 794721a
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ public ApplicationInsightsLogger(string? categoryName, IApplicationInsights appl
/// <summary>Include scope information in CustomDimensions</summary>
public bool IncludeScopes { get; set; }

/// <summary>Min LogLevel to write to app insights</summary>
public LogLevel MinLogLevel { get; set; }

/// <summary>
/// <para>Callback that will be called before customDimensions are set</para>
/// <para>This allows enriching customDimensions with values that should apply to all log lines</para>
Expand Down Expand Up @@ -86,7 +89,7 @@ public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Except
}

/// <inheritdoc />
public bool IsEnabled(LogLevel logLevel) => logLevel != LogLevel.None;
public bool IsEnabled(LogLevel logLevel) => this.MinLogLevel <= logLevel && logLevel != LogLevel.None;

/// <inheritdoc />
public IDisposable BeginScope<TState>(TState state) => ScopeProvider.Push(state);

Check warning on line 95 in src/BlazorApplicationInsights/Logging/ApplicationInsightsLogger.cs

View workflow job for this annotation

GitHub Actions / Run Tests (samples/BlazorApplicationInsights.Sample.Wasm)

Nullability in constraints for type parameter 'TState' of method 'ApplicationInsightsLogger.BeginScope<TState>(TState)' doesn't match the constraints for type parameter 'TState' of interface method 'ILogger.BeginScope<TState>(TState)'. Consider using an explicit interface implementation instead.

Check warning on line 95 in src/BlazorApplicationInsights/Logging/ApplicationInsightsLogger.cs

View workflow job for this annotation

GitHub Actions / Run Tests (samples/BlazorApplicationInsights.Sample.Wasm6)

Nullability in constraints for type parameter 'TState' of method 'ApplicationInsightsLogger.BeginScope<TState>(TState)' doesn't match the constraints for type parameter 'TState' of interface method 'ILogger.BeginScope<TState>(TState)'. Consider using an explicit interface implementation instead.

Check warning on line 95 in src/BlazorApplicationInsights/Logging/ApplicationInsightsLogger.cs

View workflow job for this annotation

GitHub Actions / Create Release

Nullability in constraints for type parameter 'TState' of method 'ApplicationInsightsLogger.BeginScope<TState>(TState)' doesn't match the constraints for type parameter 'TState' of interface method 'ILogger.BeginScope<TState>(TState)'. Consider using an explicit interface implementation instead.

Check warning on line 95 in src/BlazorApplicationInsights/Logging/ApplicationInsightsLogger.cs

View workflow job for this annotation

GitHub Actions / Create Release

Nullability in constraints for type parameter 'TState' of method 'ApplicationInsightsLogger.BeginScope<TState>(TState)' doesn't match the constraints for type parameter 'TState' of interface method 'ILogger.BeginScope<TState>(TState)'. Consider using an explicit interface implementation instead.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.ComponentModel;
using System.Text.Json.Serialization;
using JetBrains.Annotations;
using Microsoft.Extensions.Logging;

// ReSharper disable once CheckNamespace
namespace BlazorApplicationInsights;
Expand All @@ -19,8 +20,12 @@ public class ApplicationInsightsLoggerOptions
/// <summary>Include scope information in customDimensions</summary>
public bool IncludeScopes { get; set; } = true;

/// <summary>Min LogLevel to write to app insights defaults to Trace aka Verbose</summary>
public LogLevel MinLogLevel { get; set; } = LogLevel.Trace;

[JsonIgnore]
[Obsolete("Not part of the stable API")]
[EditorBrowsable(EditorBrowsableState.Never)]

public Action<Dictionary<string, object?>>? EnrichCallback { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ private ApplicationInsightsLogger CreateLoggerInstance(string categoryName)
return new ApplicationInsightsLogger(categoryName, _applicationInsights)
{
ScopeProvider = GetScopeProvider(),
MinLogLevel = _options.MinLogLevel,
IncludeScopes = _options.IncludeScopes,
IncludeCategoryName = _options.IncludeCategoryName,

Expand Down Expand Up @@ -98,6 +99,7 @@ private void ReloadOptions(ApplicationInsightsLoggerOptions options)
logger.ScopeProvider = scopeProvider;
logger.IncludeCategoryName = options.IncludeCategoryName;
logger.IncludeScopes = options.IncludeScopes;
logger.MinLogLevel = options.MinLogLevel;

#pragma warning disable 618
logger.EnrichmentCallback = _enrichmentCallback;
Expand Down

0 comments on commit 794721a

Please sign in to comment.