8 changed files with 696 additions and 26 deletions
@ -0,0 +1,96 @@ |
|||||
|
# LINGYUN.Abp.Logging |
||||
|
|
||||
|
Basic logging module |
||||
|
|
||||
|
Defines the ILoggingManager interface for implementing log information queries. |
||||
|
|
||||
|
[简体中文](./README.md) |
||||
|
|
||||
|
## Features |
||||
|
|
||||
|
* Provides unified logging query interface ILoggingManager |
||||
|
* Supports various log field queries, including timestamp, log level, message, etc. |
||||
|
* Supports exception information recording and querying |
||||
|
* Supports rich log field information such as machine name, environment name, application name, etc. |
||||
|
* Supports pagination query and count statistics |
||||
|
|
||||
|
## Module Reference |
||||
|
|
||||
|
```csharp |
||||
|
[DependsOn(typeof(AbpLoggingModule))] |
||||
|
public class YouProjectModule : AbpModule |
||||
|
{ |
||||
|
// other |
||||
|
} |
||||
|
``` |
||||
|
|
||||
|
## Configuration |
||||
|
|
||||
|
Configure in `appsettings.json`: |
||||
|
|
||||
|
```json |
||||
|
{ |
||||
|
"Logging": { |
||||
|
"MachineName": "your-machine-name", |
||||
|
"EnvironmentName": "environment-name" |
||||
|
} |
||||
|
} |
||||
|
``` |
||||
|
|
||||
|
## Basic Usage |
||||
|
|
||||
|
1. Inject ILoggingManager interface: |
||||
|
```csharp |
||||
|
public class YourService |
||||
|
{ |
||||
|
private readonly ILoggingManager _loggingManager; |
||||
|
|
||||
|
public YourService(ILoggingManager loggingManager) |
||||
|
{ |
||||
|
_loggingManager = loggingManager; |
||||
|
} |
||||
|
} |
||||
|
``` |
||||
|
|
||||
|
2. Query logs: |
||||
|
```csharp |
||||
|
// Get log list |
||||
|
var logs = await _loggingManager.GetListAsync( |
||||
|
startTime: DateTime.Now.AddDays(-1), |
||||
|
maxResultCount: 10, |
||||
|
skipCount: 0, |
||||
|
level: LogLevel.Error |
||||
|
); |
||||
|
|
||||
|
// Get log count |
||||
|
var count = await _loggingManager.GetCountAsync( |
||||
|
startTime: DateTime.Now.AddDays(-1), |
||||
|
level: LogLevel.Error |
||||
|
); |
||||
|
|
||||
|
// Get single log |
||||
|
var log = await _loggingManager.GetAsync(id); |
||||
|
``` |
||||
|
|
||||
|
## Log Field Description |
||||
|
|
||||
|
* TimeStamp - Log timestamp |
||||
|
* Level - Log level |
||||
|
* Message - Log message |
||||
|
* Fields - Log field information |
||||
|
* Id - Log unique identifier |
||||
|
* MachineName - Machine name |
||||
|
* Environment - Environment name |
||||
|
* Application - Application name |
||||
|
* Context - Context |
||||
|
* ActionId - Action ID |
||||
|
* ActionName - Action name |
||||
|
* RequestId - Request ID |
||||
|
* RequestPath - Request path |
||||
|
* ConnectionId - Connection ID |
||||
|
* CorrelationId - Correlation ID |
||||
|
* ClientId - Client ID |
||||
|
* UserId - User ID |
||||
|
* ProcessId - Process ID |
||||
|
* ThreadId - Thread ID |
||||
|
* Exceptions - Exception information list |
||||
@ -0,0 +1,91 @@ |
|||||
|
# LINGYUN.Abp.Serilog.Enrichers.Application |
||||
|
|
||||
|
[简体中文](./README.md) | English |
||||
|
|
||||
|
A Serilog enricher that adds application identifier to log properties. |
||||
|
|
||||
|
## Features |
||||
|
|
||||
|
* Adds application name to log events |
||||
|
* Configurable application name field |
||||
|
* Support for both code-based and configuration-based setup |
||||
|
* Caches log event property for better performance |
||||
|
* Seamless integration with Serilog |
||||
|
|
||||
|
## Module Dependencies |
||||
|
|
||||
|
```csharp |
||||
|
[DependsOn(typeof(AbpSerilogEnrichersApplicationModule))] |
||||
|
public class YouProjectModule : AbpModule |
||||
|
{ |
||||
|
public override void PreConfigureServices(ServiceConfigurationContext context) |
||||
|
{ |
||||
|
AbpSerilogEnrichersConsts.ApplicationName = "demo-app"; |
||||
|
} |
||||
|
} |
||||
|
``` |
||||
|
|
||||
|
## Configuration Options |
||||
|
|
||||
|
The following are field constants that need to be explicitly changed: |
||||
|
|
||||
|
* AbpSerilogEnrichersConsts.ApplicationNamePropertyName - Used to customize the name of the ApplicationName field |
||||
|
* AbpSerilogEnrichersConsts.ApplicationName - The name of the current application to be identified in logs |
||||
|
|
||||
|
## Usage |
||||
|
|
||||
|
### Code-based Configuration |
||||
|
|
||||
|
```csharp |
||||
|
Log.Logger = new LoggerConfiguration() |
||||
|
.Enrich.WithApplicationName() |
||||
|
// ...other configuration... |
||||
|
.CreateLogger(); |
||||
|
``` |
||||
|
|
||||
|
### JSON Configuration |
||||
|
|
||||
|
```json |
||||
|
{ |
||||
|
"Serilog": { |
||||
|
"MinimumLevel": { |
||||
|
"Default": "Information" |
||||
|
}, |
||||
|
"Enrich": [ "WithApplicationName" ] |
||||
|
} |
||||
|
} |
||||
|
``` |
||||
|
|
||||
|
## Implementation Details |
||||
|
|
||||
|
The enricher adds a property named "ApplicationName" (configurable) to each log event with the value specified in `AbpSerilogEnrichersConsts.ApplicationName`. The property is cached for better performance. |
||||
|
|
||||
|
## Best Practices |
||||
|
|
||||
|
1. Set the application name early in your application's startup: |
||||
|
```csharp |
||||
|
public override void PreConfigureServices(ServiceConfigurationContext context) |
||||
|
{ |
||||
|
AbpSerilogEnrichersConsts.ApplicationName = "your-app-name"; |
||||
|
} |
||||
|
``` |
||||
|
|
||||
|
2. Use a consistent naming convention for your applications to make log filtering easier. |
||||
|
|
||||
|
3. Consider setting the application name through configuration: |
||||
|
```json |
||||
|
{ |
||||
|
"App": { |
||||
|
"Name": "your-app-name" |
||||
|
} |
||||
|
} |
||||
|
``` |
||||
|
```csharp |
||||
|
AbpSerilogEnrichersConsts.ApplicationName = configuration["App:Name"]; |
||||
|
``` |
||||
|
|
||||
|
## Notes |
||||
|
|
||||
|
1. The application name is static once set and will be the same for all log entries from the application. |
||||
|
2. The enricher uses property caching to improve performance. |
||||
|
3. The property will only be added if it doesn't already exist in the log event. |
||||
@ -0,0 +1,113 @@ |
|||||
|
# LINGYUN.Abp.Serilog.Enrichers.UniqueId |
||||
|
|
||||
|
[简体中文](./README.md) | English |
||||
|
|
||||
|
A Serilog enricher that adds unique identifiers to log properties using the Snowflake algorithm. |
||||
|
|
||||
|
## Features |
||||
|
|
||||
|
* Adds unique identifier to each log event |
||||
|
* Uses Snowflake algorithm for distributed unique ID generation |
||||
|
* Supports custom Snowflake algorithm configuration |
||||
|
* Supports both code-based and configuration-based setup |
||||
|
* Seamless integration with Serilog |
||||
|
|
||||
|
## Module Dependencies |
||||
|
|
||||
|
```csharp |
||||
|
[DependsOn(typeof(AbpSerilogEnrichersUniqueIdModule))] |
||||
|
public class YouProjectModule : AbpModule |
||||
|
{ |
||||
|
// other |
||||
|
} |
||||
|
``` |
||||
|
|
||||
|
## Configuration Options |
||||
|
|
||||
|
### Constants |
||||
|
|
||||
|
* AbpSerilogUniqueIdConsts.UniqueIdPropertyName - Name of the unique identifier field, defaults to "UniqueId" |
||||
|
|
||||
|
### Snowflake Configuration |
||||
|
|
||||
|
Configure Snowflake algorithm parameters through `AbpSerilogEnrichersUniqueIdOptions`: |
||||
|
|
||||
|
```json |
||||
|
{ |
||||
|
"UniqueId": { |
||||
|
"Snowflake": { |
||||
|
"WorkerId": 1, // Worker machine ID |
||||
|
"DatacenterId": 1, // Datacenter ID |
||||
|
"Sequence": 0, // Sequence number |
||||
|
"BaseTime": "2020-01-01 00:00:00" // Base time |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
``` |
||||
|
|
||||
|
## Usage |
||||
|
|
||||
|
### Code-based Configuration |
||||
|
|
||||
|
```csharp |
||||
|
Log.Logger = new LoggerConfiguration() |
||||
|
.Enrich.WithUniqueId() |
||||
|
// ...other configuration... |
||||
|
.CreateLogger(); |
||||
|
``` |
||||
|
|
||||
|
### JSON Configuration |
||||
|
|
||||
|
```json |
||||
|
{ |
||||
|
"Serilog": { |
||||
|
"MinimumLevel": { |
||||
|
"Default": "Information" |
||||
|
}, |
||||
|
"Enrich": [ "WithUniqueId" ] |
||||
|
} |
||||
|
} |
||||
|
``` |
||||
|
|
||||
|
## Implementation Details |
||||
|
|
||||
|
This enricher uses the Snowflake algorithm to generate a unique ID for each log event. The Snowflake algorithm features: |
||||
|
|
||||
|
* Generates 64-bit long integer IDs |
||||
|
* ID consists of timestamp, datacenter ID, worker machine ID, and sequence number |
||||
|
* Ensures uniqueness in distributed environments |
||||
|
* Time-based ordering |
||||
|
|
||||
|
## Best Practices |
||||
|
|
||||
|
1. Configure worker ID and datacenter ID appropriately to avoid conflicts in distributed environments: |
||||
|
```json |
||||
|
{ |
||||
|
"UniqueId": { |
||||
|
"Snowflake": { |
||||
|
"WorkerId": 1, |
||||
|
"DatacenterId": 1 |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
``` |
||||
|
|
||||
|
2. Set an appropriate base time to maximize the available time range for IDs: |
||||
|
```json |
||||
|
{ |
||||
|
"UniqueId": { |
||||
|
"Snowflake": { |
||||
|
"BaseTime": "2020-01-01 00:00:00" |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
``` |
||||
|
|
||||
|
3. Use the UniqueId field for precise log event location in queries. |
||||
|
|
||||
|
## Notes |
||||
|
|
||||
|
1. IDs generated by the Snowflake algorithm are roughly increasing but not strictly monotonic |
||||
|
2. Worker ID and datacenter ID must be unique within the cluster |
||||
|
3. Base time cannot be modified once set, as it may cause ID duplicates |
||||
|
4. Each log event generates a new unique ID, which may incur some performance overhead |
||||
@ -0,0 +1,113 @@ |
|||||
|
# LINGYUN.Abp.Serilog.Enrichers.UniqueId |
||||
|
|
||||
|
简体中文 | [English](./README.EN.md) |
||||
|
|
||||
|
日志属性添加唯一标识,使用雪花算法生成唯一ID。 |
||||
|
|
||||
|
## 功能特性 |
||||
|
|
||||
|
* 为每个日志事件添加唯一标识 |
||||
|
* 基于雪花算法(Snowflake)生成分布式唯一ID |
||||
|
* 支持自定义雪花算法配置 |
||||
|
* 支持代码配置和JSON配置两种方式 |
||||
|
* 与Serilog无缝集成 |
||||
|
|
||||
|
## 模块引用 |
||||
|
|
||||
|
```csharp |
||||
|
[DependsOn(typeof(AbpSerilogEnrichersUniqueIdModule))] |
||||
|
public class YouProjectModule : AbpModule |
||||
|
{ |
||||
|
// other |
||||
|
} |
||||
|
``` |
||||
|
|
||||
|
## 配置项 |
||||
|
|
||||
|
### 常量配置 |
||||
|
|
||||
|
* AbpSerilogUniqueIdConsts.UniqueIdPropertyName - 唯一标识字段的名称,默认为"UniqueId" |
||||
|
|
||||
|
### 雪花算法配置 |
||||
|
|
||||
|
通过 `AbpSerilogEnrichersUniqueIdOptions` 配置雪花算法参数: |
||||
|
|
||||
|
```json |
||||
|
{ |
||||
|
"UniqueId": { |
||||
|
"Snowflake": { |
||||
|
"WorkerId": 1, // 工作机器ID |
||||
|
"DatacenterId": 1, // 数据中心ID |
||||
|
"Sequence": 0, // 序列号 |
||||
|
"BaseTime": "2020-01-01 00:00:00" // 基准时间 |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
``` |
||||
|
|
||||
|
## 使用方法 |
||||
|
|
||||
|
### 代码配置方式 |
||||
|
|
||||
|
```csharp |
||||
|
Log.Logger = new LoggerConfiguration() |
||||
|
.Enrich.WithUniqueId() |
||||
|
// ...其他配置... |
||||
|
.CreateLogger(); |
||||
|
``` |
||||
|
|
||||
|
### JSON配置方式 |
||||
|
|
||||
|
```json |
||||
|
{ |
||||
|
"Serilog": { |
||||
|
"MinimumLevel": { |
||||
|
"Default": "Information" |
||||
|
}, |
||||
|
"Enrich": [ "WithUniqueId" ] |
||||
|
} |
||||
|
} |
||||
|
``` |
||||
|
|
||||
|
## 实现细节 |
||||
|
|
||||
|
该enricher使用雪花算法为每个日志事件生成一个唯一的ID。雪花算法的特点是: |
||||
|
|
||||
|
* 生成的ID是64位的长整型 |
||||
|
* ID由时间戳、数据中心ID、工作机器ID和序列号组成 |
||||
|
* 保证在分布式环境下的唯一性 |
||||
|
* 基于时间戳的有序性 |
||||
|
|
||||
|
## 最佳实践 |
||||
|
|
||||
|
1. 合理配置工作机器ID和数据中心ID,避免在分布式环境中产生冲突: |
||||
|
```json |
||||
|
{ |
||||
|
"UniqueId": { |
||||
|
"Snowflake": { |
||||
|
"WorkerId": 1, |
||||
|
"DatacenterId": 1 |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
``` |
||||
|
|
||||
|
2. 设置合适的基准时间以最大化ID的可用时间范围: |
||||
|
```json |
||||
|
{ |
||||
|
"UniqueId": { |
||||
|
"Snowflake": { |
||||
|
"BaseTime": "2020-01-01 00:00:00" |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
``` |
||||
|
|
||||
|
3. 在日志查询时可以使用UniqueId字段进行精确定位。 |
||||
|
|
||||
|
## 注意事项 |
||||
|
|
||||
|
1. 雪花算法生成的ID是趋势递增的,但不保证严格递增 |
||||
|
2. 工作机器ID和数据中心ID在集群中必须唯一 |
||||
|
3. 基准时间一旦设置就不能修改,否则可能导致ID重复 |
||||
|
4. 每个日志事件都会生成新的唯一ID,这可能会增加一些性能开销 |
||||
Loading…
Reference in new issue