ZoppaLoggingExtensions 1.0.3

dotnet add package ZoppaLoggingExtensions --version 1.0.3
                    
NuGet\Install-Package ZoppaLoggingExtensions -Version 1.0.3
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="ZoppaLoggingExtensions" Version="1.0.3" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="ZoppaLoggingExtensions" Version="1.0.3" />
                    
Directory.Packages.props
<PackageReference Include="ZoppaLoggingExtensions" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add ZoppaLoggingExtensions --version 1.0.3
                    
#r "nuget: ZoppaLoggingExtensions, 1.0.3"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package ZoppaLoggingExtensions@1.0.3
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=ZoppaLoggingExtensions&version=1.0.3
                    
Install as a Cake Addin
#tool nuget:?package=ZoppaLoggingExtensions&version=1.0.3
                    
Install as a Cake Tool

説明

Microsoft.Extensions.Logging に従って抽象化したログ出力機能を提供します。
これは、ZoppaDsqlを更新したZoppaDSqlMapperにおける標準ログ機能として使用されています。
高効率を目指していないため、ログ出力の効率を気にする場合は別のライブラリの使用を検討してください。

using Microsoft.Extensions.Logging;
using System.Runtime.InteropServices;
using ZoppaLoggingExtensions;

// ログ出力のためのファクトリを作成
using var loggerFactory = LoggerFactory.Create(builder => {
    builder.AddZoppaLogging(
        (config) => config.MinimumLogLevel = LogLevel.Trace
    );
    builder.SetMinimumLevel(LogLevel.Trace);
});

ILogger logger = loggerFactory.CreateLogger<Program>();

// ログ出力の例
using (logger.BeginScope("scope start {a}", 100)) {
    logger.ZLog<Program>().LogDebug(1, "Does this line get hit? {h} {b}", 100, 200);
    using (logger.ZLog<Program>().BeginScope("2")) {
        logger.ZLog<Program>().LogDebug(3, "Nothing to see here.");
        logger.ZLog<Program>().LogDebug(5, "Warning... that was odd.");
    }
    logger.ZLog<Program>().LogDebug(7, "Oops, there was an error.");
}
logger.ZLog<Program>().LogDebug(5, "== 120.");

Microsoft.Extensions.Logging の初期化ルールに従い、ILoggingBuilderAddZoppaLogging の拡張メソッドを使ってログの出力先の設定を行ってください。
AddZoppaLogging には、ZoppaLoggingConfiguration を設定するための Action<ZoppaLoggingConfiguration> を渡すことができます。
ZoppaLoggingConfiguration には、出力ファイルを指定する DefaultLogFile と、カテゴリごとの出力ファイルを指定する LogFileByCategory があります。 注意 ログレベルを指定する MinimumLogLevelILoggingBuilderSetMinimumLevelの値以上を指定するようにしてください。

汎用ホストを使用する例を以下に示します。

    Private Shared Async Function MainAsync(args As String()) As Task
        ' 汎用ホストを作成
        Dim builder = Microsoft.Extensions.Hosting.Host.CreateApplicationBuilder(args)

        ' 汎用ホストを使用して初期化
        builder.Logging.ClearProviders()
        builder.Logging.AddZoppaLogging(
            Sub(config)
                config.MinimumLogLevel = LogLevel.Trace
            End Sub
        )
        builder.Logging.SetMinimumLevel(LogLevel.Trace)

        ' 汎用ホストをビルド、ログ出力
        Using host = builder.Build()
            Dim loggerFactory = host.Services.GetRequiredService(Of ILoggerFactory)()

            Dim logger = loggerFactory.CreateLogger("Test")

            logger.ZLog(Of Program).LogDebug(1, "Does this line get hit?")
            logger.ZLog(Of Program).LogInformation(3, "Nothing to see here.")
            logger.ZLog(Of Program).LogWarning(5, "Warning... that was odd.")
            logger.ZLog(Of Program).LogError(7, "Oops, there was an error.")
            logger.ZLog(Of Program).LogTrace(5, "== 120.")

            Await host.RunAsync()
        End Using
    End Function

汎用ホストを使用する場合は、appsettings.jsonで出力するファイルを指定することができます。

{
  "Logging": {
	"ZoppaLogging": {
	  "DefaultLogFile": "log.txt",
	  "LogFileByCategory": {
		"Test": "test.txt"
	  }
	}
  }
}

最後に ILogger の拡張メソッド ZLog を使用すると呼び出し元のメソッド名と行番号を出力することができます。

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (3)

Showing the top 3 NuGet packages that depend on ZoppaLoggingExtensions:

Package Downloads
ZoppaDSqlMapper

Package Description

ZoppaDSqlReplace

Package Description

ZoppaDSqlCompiler

Package Description

GitHub repositories

This package is not used by any popular GitHub repositories.