Skip to content

Commit

Permalink
CallerMemberName -> MemberName
Browse files Browse the repository at this point in the history
  • Loading branch information
neuecc committed Jan 26, 2024
1 parent 58bfbda commit 20571b9
Show file tree
Hide file tree
Showing 12 changed files with 164 additions and 117 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ Formats using messagepack are supported in an additional package.

todo

LogInfo ?
LogInfo
---

| Name | Description |
Expand All @@ -412,6 +412,9 @@ LogInfo ?
| `EventId EventId` | EventId of `Microsoft.Extensions.Logging` |
| `Exception? Exception` | Exception given as argument when logging. |
| `LogScopeState? ScopeState` | Additional properties set by `ILogger.BeginScope(...)` (if ZLoggerOptions.IncludeScopes = true) |
| `string? MemberName` | Caller MemberName |
| `string? FilePath` | Caller FilePath |
| `int LineNumber` | Caller LineNumber |


KeyNameMutator
Expand Down
46 changes: 25 additions & 21 deletions sandbox/ConsoleApp/ConsoleApp.csproj
Original file line number Diff line number Diff line change
@@ -1,29 +1,33 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="ConsoleAppFramework" Version="4.2.4" />
<PackageReference Include="JetBrains.Profiler.Api" Version="1.4.0" />
<PackageReference Include="Microsoft.CodeAnalysis.BannedApiAnalyzers" Version="2.9.8">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.0" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="ConsoleAppFramework" Version="4.2.4" />
<PackageReference Include="JetBrains.Profiler.Api" Version="1.4.0" />
<PackageReference Include="Microsoft.CodeAnalysis.BannedApiAnalyzers" Version="2.9.8">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.0" />
</ItemGroup>

<ItemGroup>
<AdditionalFiles Include="BannedSymbols.txt" />
</ItemGroup>
<ItemGroup>
<AdditionalFiles Include="BannedSymbols.txt" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\ZLogger\ZLogger.csproj" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\ZLogger\ZLogger.csproj" />
<ProjectReference Include="..\..\src\ZLogger.Generator\ZLogger.Generator.csproj">
<OutputItemType>Analyzer</OutputItemType>
<ReferenceOutputAssembly>false</ReferenceOutputAssembly>
</ProjectReference>
</ItemGroup>

</Project>
22 changes: 19 additions & 3 deletions sandbox/ConsoleApp/Program.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
using Microsoft.Extensions.Logging;
using System.Runtime.CompilerServices;
using ZLogger;

using var factory = LoggerFactory.Create(logging =>
{
// Add ZLogger provider to ILoggingBuilder
logging.AddZLoggerConsole();
logging.AddZLoggerConsole(options =>
{
options.UsePlainTextFormatter(formatter =>
{
// formatter.SetPrefixFormatter($"foo", (template, info) => info.CallerLineNumber
});
});

// Output Structured Logging, setup options
logging.AddZLoggerConsole(options => options.UseJsonFormatter(formatter =>
{
formatter.IncludeProperties = IncludeProperties.ParameterKeyValues;
formatter.IncludeProperties = IncludeProperties.ParameterKeyValues | IncludeProperties.MemberName | IncludeProperties.FilePath | IncludeProperties.LineNumber;
}));
});

Expand All @@ -21,13 +28,16 @@
// Use **Z**Log method and string interpolation to log message
logger.ZLogInformation($"Hello my name is {name}, {age} years old.");

LogLog.Foo(logger, "tako", "huga", 1000);


// Output messages:
// Hello my name is John, 33 years old.
// {"Timestamp":"2023-12-04T19:39:59.9237682+09:00","LogLevel":"Information","Category":"Program","Message":"Hello my name is John, 33 years old.","name":"John","age":33}



public class MyClass
public partial class MyClass
{
// get ILogger<T> from DI.
readonly ILogger<MyClass> logger;
Expand Down Expand Up @@ -71,4 +81,10 @@ public void Foo(string name, string city, int age)
// json:
// {"Timestamp":"2023-12-01T16:59:29.908126+09:00","LogLevel":"Information","Category":"my","Message":"users: [{\u0022Id\u0022:1,\u0022Name\u0022:\u0022Alice\u0022},{\u0022Id\u0022:1,\u0022Name\u0022:\u0022Bob\u0022}]","users":[{"Id":1,"Name":"Alice"},{"Id":1,"Name":"Bob"}]}
}
}

public static partial class LogLog
{
[ZLoggerMessage(LogLevel.Information, "foo is {name} {city} {age}")]
public static partial void Foo(ILogger logger, string name, string city, int age, [CallerMemberName] string? memberName = null, [CallerFilePath] string? filePath = null, [CallerLineNumber] int lineNumber = 0);
}
2 changes: 1 addition & 1 deletion sandbox/GeneratorSandbox/GeneratedMock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public static void CouldNotOpenSocketWithCaller(this ILogger<FooBarBaz> logger,

}

file readonly struct CouldNotOpenSocketState : IZLoggerFormattable, ICallerTracable, IReadOnlyList<KeyValuePair<string, object?>>
file readonly struct CouldNotOpenSocketState : IZLoggerFormattable, ICallerTraceable, IReadOnlyList<KeyValuePair<string, object?>>
{
static readonly JsonEncodedText _jsonParameter_hostName = JsonEncodedText.Encode("hostName");
static readonly JsonEncodedText _jsonParameter_ipAddress = JsonEncodedText.Encode("ipAddress");
Expand Down
2 changes: 1 addition & 1 deletion src/ZLogger.Generator/ZLoggerGenerator.Emitter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public void EmitStructState(LogMethodDeclaration method)
.StringJoinNewLine();

sb.AppendLine($$"""
readonly struct {{stateTypeName}} : IZLoggerFormattable, ICallerTracable, IReadOnlyList<KeyValuePair<string, object?>>
readonly struct {{stateTypeName}} : IZLoggerFormattable, ICallerTraceable, IReadOnlyList<KeyValuePair<string, object?>>
{
public string? CallerMemberName { get; }
public string? CallerFilePath { get; }
Expand Down
30 changes: 27 additions & 3 deletions src/ZLogger/Formatters/SystemTextJsonZLoggerFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,12 @@ public enum IncludeProperties
Exception = 1 << 6,
ScopeKeyValues = 1 << 7,
ParameterKeyValues = 1 << 8,
MemberName = 1 << 9, // added in 2.2.0
FilePath = 1 << 10, // added in 2.2.0
LineNumber = 1 << 11, // added in 2.2.0

Default = Timestamp | LogLevel | CategoryName | Message | Exception | ScopeKeyValues | ParameterKeyValues,
All = Timestamp | LogLevel | CategoryName | EventIdValue | EventIdName | Message | Exception | ScopeKeyValues | ParameterKeyValues
All = Timestamp | LogLevel | CategoryName | EventIdValue | EventIdName | Message | Exception | ScopeKeyValues | ParameterKeyValues | MemberName | FilePath | LineNumber
}
}

Expand All @@ -63,7 +67,11 @@ public record JsonPropertyNames(
JsonEncodedText LogLevelWarning,
JsonEncodedText LogLevelError,
JsonEncodedText LogLevelCritical,
JsonEncodedText LogLevelNone
JsonEncodedText LogLevelNone,

JsonEncodedText MemberName,
JsonEncodedText FilePath,
JsonEncodedText LineNumber
)
{
public static readonly JsonPropertyNames Default = new(
Expand All @@ -86,7 +94,11 @@ JsonEncodedText LogLevelNone
LogLevelWarning: JsonEncodedText.Encode(nameof(Microsoft.Extensions.Logging.LogLevel.Warning)),
LogLevelError: JsonEncodedText.Encode(nameof(Microsoft.Extensions.Logging.LogLevel.Error)),
LogLevelCritical: JsonEncodedText.Encode(nameof(Microsoft.Extensions.Logging.LogLevel.Critical)),
LogLevelNone: JsonEncodedText.Encode(nameof(Microsoft.Extensions.Logging.LogLevel.None))
LogLevelNone: JsonEncodedText.Encode(nameof(Microsoft.Extensions.Logging.LogLevel.None)),

MemberName: JsonEncodedText.Encode(nameof(LogInfo.MemberName)),
FilePath: JsonEncodedText.Encode(nameof(LogInfo.FilePath)),
LineNumber: JsonEncodedText.Encode(nameof(LogInfo.LineNumber))
);
}

Expand Down Expand Up @@ -232,6 +244,18 @@ void FormatLogInfo(Utf8JsonWriter jsonWriter, LogInfo info)
WriteException(jsonWriter, ex);
}
}
if ((flag & IncludeProperties.MemberName) != 0)
{
jsonWriter.WriteString(JsonPropertyNames.MemberName, info.MemberName);
}
if ((flag & IncludeProperties.FilePath) != 0)
{
jsonWriter.WriteString(JsonPropertyNames.FilePath, info.FilePath);
}
if ((flag & IncludeProperties.LineNumber) != 0)
{
jsonWriter.WriteNumber(JsonPropertyNames.LineNumber, info.LineNumber);
}
}

void WriteException(Utf8JsonWriter jsonWriter, Exception? ex)
Expand Down
2 changes: 1 addition & 1 deletion src/ZLogger/IZLoggerFormattable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public interface IReferenceCountable
void Release();
}

public interface ICallerTracable
public interface ICallerTraceable
{
string? CallerMemberName { get; }
string? CallerFilePath { get; }
Expand Down
8 changes: 4 additions & 4 deletions src/ZLogger/LogInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@

namespace ZLogger;

public readonly struct LogInfo(LogCategory category, Timestamp timestamp, LogLevel logLevel, EventId eventId, Exception? exception, LogScopeState? scopeState, string? callerMemberName = null, string? callerFilePath = null, int callerLineNumber = 0)
public readonly struct LogInfo(LogCategory category, Timestamp timestamp, LogLevel logLevel, EventId eventId, Exception? exception, LogScopeState? scopeState, string? memberName = null, string? filePath = null, int lineNumber = 0)
{
public readonly LogCategory Category = category;
public readonly Timestamp Timestamp = timestamp;
public readonly LogLevel LogLevel = logLevel;
public readonly EventId EventId = eventId;
public readonly Exception? Exception = exception;
public readonly LogScopeState? ScopeState = scopeState;
public readonly string? CallerMemberName = callerMemberName;
public readonly string? CallerFilePath = callerFilePath;
public readonly int CallerLineNumber = callerLineNumber;
public readonly string? MemberName = memberName;
public readonly string? FilePath = filePath;
public readonly int LineNumber = lineNumber;
}

public readonly struct LogCategory
Expand Down
4 changes: 2 additions & 2 deletions src/ZLogger/LogStates/InterpolatedStringLogState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace ZLogger.LogStates;
public sealed class InterpolatedStringLogState :
IZLoggerFormattable,
IReferenceCountable,
ICallerTracable,
ICallerTraceable,
IObjectPoolNode<InterpolatedStringLogState>,
IEnumerable<KeyValuePair<string, object?>>
{
Expand Down Expand Up @@ -186,7 +186,7 @@ public void Dispose() { }
}

[StructLayout(LayoutKind.Auto)]
public readonly struct VersionedLogState : IZLoggerEntryCreatable, IReferenceCountable, ICallerTracable, IEnumerable<KeyValuePair<string, object?>>
public readonly struct VersionedLogState : IZLoggerEntryCreatable, IReferenceCountable, ICallerTraceable, IEnumerable<KeyValuePair<string, object?>>
{
readonly InterpolatedStringLogState state;
readonly int version;
Expand Down
Loading

0 comments on commit 20571b9

Please sign in to comment.