C#でカスタムロギングを使用する方法
カスタムロギングとは、アプリケーションやシステムの特定のニーズと要件に合わせてロギングシステムを実装することを指します。 ソフトウェアの動作中に生成される情報、イベント、およびメッセージを記録するために、ログファイルを作成および使用することが含まれます。
IronPDFを始めましょう
今日から無料トライアルでIronPDFをあなたのプロジェクトで使い始めましょう。
C#でカスタムロギングを使用する方法
- NuGetからIronPdfをダウンロードする
- 次のようにセットしてください ロギングモード プロパティをLoggingModes.Customに設定
- 次の内容を日本語に翻訳します: Assign the 担当者を割り当てる カスタムロガー カスタム ロガー オブジェクトへのプロパティ
- すべてのログメッセージはカスタムロガーに転送されます。
- ログメッセージを出力してログを表示します。
カスタムロギング例
カスタムログ機能を利用するには、LoggingMode プロパティを LoggingModes.Custom に変更してください。 その後、作成したカスタムロガークラスにCustomLoggerプロパティを割り当てます。
:path=/static-assets/pdf/content-code-examples/how-to/custom-logging-custom-logging.cs
IronSoftware.Logger.LoggingMode = IronSoftware.Logger.LoggingModes.Custom;
IronSoftware.Logger.CustomLogger = new CustomLoggerClass("logging");
IronSoftware.Logger.LoggingMode = IronSoftware.Logger.LoggingModes.Custom
IronSoftware.Logger.CustomLogger = New CustomLoggerClass("logging")
IronPDF のログはカスタムロガーオブジェクトに転送されます。 メッセージはIronPDFロガーのものと同一のままです。 それらは単にカスタムロガーにリレーされます。 カスタムロガーがメッセージを管理する方法は、カスタムロガーデザイナーによって決定されます。 次のカスタムロガークラスを例として使用しましょう。
:path=/static-assets/pdf/content-code-examples/how-to/custom-logging-custom-logging-class.cs
public class CustomLoggerClass : ILogger
{
private readonly string categoryName;
public CustomLoggerClass(string categoryName)
{
this.categoryName = categoryName;
}
public IDisposable BeginScope<TState>(TState state)
{
return null;
}
public bool IsEnabled(LogLevel logLevel)
{
return true;
}
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter)
{
if (!IsEnabled(logLevel))
{
return;
}
// Implement your custom logging logic here.
string logMessage = formatter(state, exception);
// You can use 'logLevel', 'eventId', 'categoryName', and 'logMessage' to log the message as needed.
// For example, you can write it to a file, console, or another destination.
// Example: Writing to the console
Console.WriteLine($"[{logLevel}] [{categoryName}] - {logMessage}");
}
}
Public Class CustomLoggerClass
Implements ILogger
Private ReadOnly categoryName As String
Public Sub New(ByVal categoryName As String)
Me.categoryName = categoryName
End Sub
Public Function BeginScope(Of TState)(ByVal state As TState) As IDisposable
Return Nothing
End Function
Public Function IsEnabled(ByVal logLevel As LogLevel) As Boolean
Return True
End Function
Public Sub Log(Of TState)(ByVal logLevel As LogLevel, ByVal eventId As EventId, ByVal state As TState, ByVal exception As Exception, ByVal formatter As Func(Of TState, Exception, String))
If Not IsEnabled(logLevel) Then
Return
End If
' Implement your custom logging logic here.
Dim logMessage As String = formatter(state, exception)
' You can use 'logLevel', 'eventId', 'categoryName', and 'logMessage' to log the message as needed.
' For example, you can write it to a file, console, or another destination.
' Example: Writing to the console
Console.WriteLine($"[{logLevel}] [{categoryName}] - {logMessage}")
End Sub
End Class
この場合、ログメッセージに追加情報を接頭辞として付けました。
