Jc.Log 1.4.21

There is a newer version of this package available.
See the version list below for details.
dotnet add package Jc.Log --version 1.4.21                
NuGet\Install-Package Jc.Log -Version 1.4.21                
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="Jc.Log" Version="1.4.21" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Jc.Log --version 1.4.21                
#r "nuget: Jc.Log, 1.4.21"                
#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.
// Install Jc.Log as a Cake Addin
#addin nuget:?package=Jc.Log&version=1.4.21

// Install Jc.Log as a Cake Tool
#tool nuget:?package=Jc.Log&version=1.4.21                

JcLog 模块文档

概述

JcLog 是一个轻量级日志模块,用于在 C# 应用程序中进行日志记录。本文档详细介绍如何使用 JcLog 进行有效的日志管理,包括初始化、配置、日志写入和错误处理。

初始化和配置

JcLog 可以通过多种方式初始化,允许自定义日志名称和根目录。使用 JcLogOptions 进行详细配置,包括日志保留策略和是否输出到控制台。

示例

var logOptions = new JcLogOptions { LogName = "应用日志", RootFolder = "日志目录", RetentionDays = 30 };
var logger = new JcLog(logOptions);

写入日志

JcLog 提供了多种写入日志的方法,包括信息日志、警告和错误,支持将字符串或可序列化的对象作为日志内容。

示例

logger.WriteLog("这是一条信息日志");
logger.WriteError("这是一条错误日志");
logger.WriteWaring("这是一条警告日志");

解析日志

功能

  • 解析日志文件,将文本行转换为结构化的日志条目。
  • 支持提取时间戳、日志类型、消息内容,以及可选的序列化对象数据。

使用方法

解析日志文件

要解析一个日志文件,您需要创建一个 JcLogParser 实例,并调用 ParseLogFile 方法,传入日志文件的路径。

var parser = new JcLogParser();
var logEntries = parser.ParseLogFile("路径到您的日志文件");
LogEntry 结构

每个 LogEntry 对象包含以下属性:

  • Timestamp:日志条目的时间戳。
  • LogType:日志类型(如 info, warning, error)。
  • Message:日志消息内容。
  • AdditionalData:如果日志中包含了序列化的对象数据,此属性将包含该数据的动态表示。
案例

假设您有一个日志文件 app.log,格式如下:

12:34:56.789>[INFO]>这是一个信息日志
12:35:00.123>[ERROR]>这是一个错误日志>{"错误码":404,"错误信息":"未找到"}
使用 JcLogParser 解析此文件将产生以下结果
foreach(var entry in logEntries)
{
    Console.WriteLine($"时间: {entry.Timestamp}, 类型: {entry.LogType}, 消息: {entry.Message}");
    if(entry.AdditionalData != null)
    {
        Console.WriteLine($"额外数据: {entry.AdditionalData}");
    }
}

输出:

时间: 12:34:56.789, 类型: INFO, 消息: 这是一个信息日志
时间: 12:35:00.123, 类型: ERROR, 消息: 这是一个错误日志
额外数据: {"错误码":404,"错误信息":"未找到"}

错误处理和日志保留

JcLog 自动处理写入过程中的异常,保证应用程序的稳定性。通过配置 JcLogOptions 中的 RetentionDays 来管理日志文件的保留期限。

日志文件管理

JcLog 根据日期自动创建日志文件,并根据保留策略自动清理旧日志文件,优化磁盘空间使用。

结论

JcLog 提供了一个简单而强大的日志记录解决方案,适用于需要在 C# 应用程序中进行高效日志管理的场景。通过上述示例和说明,开发者可以轻松集成和使用 JcLog 进行日志记录。

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. 
.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 is compatible. 
.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 (1)

Showing the top 1 NuGet packages that depend on Jc.Log:

Package Downloads
Jc.ActionFilter.ActionLog

.net webapi 过滤器中间件:log输出

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.4.26 25 11/14/2024
1.4.25 107 7/5/2024
1.4.25-beta 82 7/5/2024
1.4.25-alpha 76 7/5/2024
1.4.24 95 6/20/2024
1.4.24-rc 87 6/20/2024
1.4.24-beta 82 6/20/2024
1.4.24-alpha 85 6/20/2024
1.4.23 149 3/14/2024
1.4.22 124 3/13/2024
1.4.21 126 3/8/2024
1.4.20 124 3/8/2024
1.4.19 186 8/28/2023
1.3.19 146 8/25/2023
1.3.18 154 8/25/2023
1.3.17 175 7/10/2023
1.2.17 231 5/11/2023
1.2.16 170 5/11/2023
1.1.16 253 3/15/2023
1.1.15 323 3/13/2023
1.1.14 248 3/7/2023
1.1.13 561 5/13/2022
1.1.12 429 4/21/2022
1.1.11 444 4/21/2022
1.1.10 429 4/16/2022
1.1.9 424 4/16/2022
1.1.8 440 4/16/2022
1.1.7 429 4/16/2022
1.1.6 426 4/16/2022
1.1.5 1,114 3/19/2022
1.0.4 790 3/18/2022
1.0.3 750 3/18/2022
1.0.2 785 3/3/2022
1.0.0 410 2/26/2022