Skip to content

Commit

Permalink
Introduced logger context (#1875)
Browse files Browse the repository at this point in the history
To improve debugging experience while integrating with Wilson
  • Loading branch information
sruke authored Jun 14, 2022
1 parent c91e758 commit 18dd906
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 28 deletions.
1 change: 1 addition & 0 deletions src/Microsoft.IdentityModel.Logging/GlobalSuppressions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@

[assembly: SuppressMessage("Design", "CA1052:Static holder types should be Static or NotInheritable", Justification = "Previously released as non-static / inheritable", Scope = "type", Target = "~T:Microsoft.IdentityModel.Logging.LogHelper")]
[assembly: SuppressMessage("Design", "CA1031:Do not catch general exception types", Justification = "Execution should not be altered for exceptions on format", Scope = "member", Target = "~M:Microsoft.IdentityModel.Logging.IdentityModelEventSource.PrepareMessage(System.Diagnostics.Tracing.EventLevel,System.String,System.Object[])~System.String")]
[assembly: SuppressMessage("Usage", "CA2227:Collection properties should be read only", Justification = "Breaking change", Scope = "member", Target = "~P:Microsoft.IdentityModel.Logging.LoggerContext.PropertyBag")]
57 changes: 57 additions & 0 deletions src/Microsoft.IdentityModel.Logging/LoggerContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;

namespace Microsoft.IdentityModel.Logging
{
/// <summary>
/// A context class that can be used to store work per request to aid with debugging.
/// </summary>
public class LoggerContext
{
/// <summary>
/// Instantiates a new <see cref="LoggerContext"/> with the default activityId.
/// </summary>
public LoggerContext()
{
}

/// <summary>
/// Instantiates a new <see cref="LoggerContext"/> with an activityId.
/// </summary>
/// <param name="activityId"></param>
public LoggerContext(Guid activityId)
{
ActivityId = activityId;
}

/// <summary>
/// Gets or set a <see cref="Guid"/> that will be used in the call to EventSource.SetCurrentThreadActivityId before logging.
/// </summary>
public Guid ActivityId { get; set; } = Guid.Empty;

/// <summary>
/// Gets or sets a boolean controlling if logs are written into the context.
/// Useful when debugging.
/// </summary>
public bool CaptureLogs { get; set; } = false;

/// <summary>
/// Gets or sets a string that helps with setting breakpoints when debugging.
/// </summary>
public string Id { get; set; } = string.Empty;

/// <summary>
/// The collection of logs associated with a request. Use <see cref="CaptureLogs"/> to control capture.
/// </summary>
public ICollection<string> Logs { get; private set; } = new Collection<string>();

/// <summary>
/// Gets or sets an <see cref="IDictionary{String, Object}"/> that enables custom extensibility scenarios.
/// </summary>
public IDictionary<string, object> PropertyBag { get; set; }
}
}
31 changes: 4 additions & 27 deletions src/Microsoft.IdentityModel.Tokens/CallContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,50 +26,27 @@
//------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using Microsoft.IdentityModel.Logging;

namespace Microsoft.IdentityModel.Tokens
{
/// <summary>
/// An opaque context used to store work when working with authentication artifacts.
/// </summary>
public class CallContext
public class CallContext : LoggerContext
{
/// <summary>
/// Instantiates a new <see cref="CallContext"/> with a default activityId.
/// </summary>
public CallContext()
public CallContext() : base()
{
}

/// <summary>
/// Instantiates a new <see cref="CallContext"/> with an activityId.
/// </summary>
public CallContext(Guid activityId)
public CallContext(Guid activityId) : base(activityId)
{
ActivityId = activityId;
}

/// <summary>
/// Gets or set a <see cref="Guid"/> that will be used in the call to EventSource.SetCurrentThreadActivityId before logging.
/// </summary>
public Guid ActivityId { get; set; } = Guid.Empty;

/// <summary>
/// Gets or sets a boolean controlling if logs are written into the context.
/// Useful when debugging.
/// </summary>
public bool CaptureLogs { get; set; } = false;

/// <summary>
/// The collection of logs associated with a request. Use <see cref="CaptureLogs"/> to control capture.
/// </summary>
public ICollection<string> Logs { get; private set; } = new Collection<string>();

/// <summary>
/// Gets or sets an <see cref="IDictionary{String, Object}"/> that enables custom extensibility scenarios.
/// </summary>
public IDictionary<string, object> PropertyBag { get; set; }
}
}
1 change: 0 additions & 1 deletion src/Microsoft.IdentityModel.Tokens/GlobalSuppressions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
[assembly: SuppressMessage("Usage", "CA2227:Collection properties should be read only", Justification = "Breaking change", Scope = "member", Target = "~P:Microsoft.IdentityModel.Tokens.SecurityTokenDescriptor.AdditionalHeaderClaims")]
[assembly: SuppressMessage("Usage", "CA2227:Collection properties should be read only", Justification = "Breaking change", Scope = "member", Target = "~P:Microsoft.IdentityModel.Tokens.SecurityTokenDescriptor.Claims")]
[assembly: SuppressMessage("Usage", "CA2227:Collection properties should be read only", Justification = "Breaking change", Scope = "member", Target = "~P:Microsoft.IdentityModel.Tokens.JsonWebKey.Oth")]
[assembly: SuppressMessage("Usage", "CA2227:Collection properties should be read only", Justification = "Breaking change", Scope = "member", Target = "~P:Microsoft.IdentityModel.Tokens.CallContext.PropertyBag")]
[assembly: SuppressMessage("Usage", "CA2227:Collection properties should be read only", Justification = "Breaking chnage", Scope = "member", Target = "~P:Microsoft.IdentityModel.Tokens.TokenValidationParameters.PropertyBag")]
[assembly: SuppressMessage("Usage", "CA2214:Do not call overridable methods in constructors", Justification = "Current design", Scope = "member", Target = "~M:Microsoft.IdentityModel.Tokens.TokenValidationParameters.#ctor(Microsoft.IdentityModel.Tokens.TokenValidationParameters)")]
[assembly: SuppressMessage("Usage", "CA2213:Disposable fields should be disposed", Justification = "Disposed through ReleaseSignatureProvider", Scope = "member", Target = "~F:Microsoft.IdentityModel.Tokens.AuthenticatedEncryptionProvider._symmetricSignatureProvider")]
Expand Down
52 changes: 52 additions & 0 deletions test/Microsoft.IdentityModel.Tokens.Tests/CallContextTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using System;
using System.CodeDom;
using Microsoft.IdentityModel.Logging;
using Microsoft.IdentityModel.TestUtils;
using Xunit;

#pragma warning disable CS3016 // Arrays as attribute arguments is not CLS-compliant

namespace Microsoft.IdentityModel.Tokens.Tests
{
public class CallContextTests
{
[Theory, MemberData(nameof(CallContextTestTheoryData))]
public void LoggerInstanceTests(CallContextTheoryData theoryData)
{
var context = new CallContext(theoryData.ActivityId) { Id = theoryData.TestId };

Assert.IsAssignableFrom<LoggerContext>(context);
Assert.Equal(theoryData.TestId, context.Id);
Assert.Equal(theoryData.ActivityId, context.ActivityId);
Assert.False(context.CaptureLogs);
Assert.Empty(context.Logs);
Assert.Null(context.PropertyBag);
}

public static TheoryData<TheoryDataBase> CallContextTestTheoryData
{
get
{
var theoryData = new TheoryData<TheoryDataBase>();

theoryData.Add(new CallContextTheoryData
{
TestId = "abdc",
ActivityId = new Guid()
});

return theoryData;
}
}
}

public class CallContextTheoryData : TheoryDataBase
{
public Guid ActivityId;
}
}

#pragma warning restore CS3016 // Arrays as attribute arguments is not CLS-compliant

0 comments on commit 18dd906

Please sign in to comment.