上传源代码版本
This commit is contained in:
18
test/LogConfigTest/LogConfigTest.csproj
Normal file
18
test/LogConfigTest/LogConfigTest.csproj
Normal file
@@ -0,0 +1,18 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration" Version="6.0.1" />
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="6.0.0" />
|
||||
<PackageReference Include="Serilog" Version="2.12.0" />
|
||||
<PackageReference Include="Serilog.AspNetCore" Version="6.1.0" />
|
||||
<PackageReference Include="Serilog.Sinks.File" Version="5.0.0" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
64
test/LogConfigTest/Program.cs
Normal file
64
test/LogConfigTest/Program.cs
Normal file
@@ -0,0 +1,64 @@
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Serilog;
|
||||
using System;
|
||||
using System.IO;
|
||||
|
||||
namespace LogConfigTest
|
||||
{
|
||||
class Program
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
try
|
||||
{
|
||||
// 加载配置文件
|
||||
var config = new ConfigurationBuilder()
|
||||
.SetBasePath(Directory.GetCurrentDirectory())
|
||||
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
|
||||
.Build();
|
||||
|
||||
// 配置Serilog
|
||||
Log.Logger = new LoggerConfiguration()
|
||||
.ReadFrom.Configuration(config)
|
||||
.CreateLogger();
|
||||
|
||||
Console.WriteLine("日志配置成功!");
|
||||
Console.WriteLine("正在测试日志写入...");
|
||||
|
||||
// 写入测试日志
|
||||
Log.Information("这是一条信息日志");
|
||||
Log.Warning("这是一条警告日志");
|
||||
Log.Error("这是一条错误日志");
|
||||
|
||||
Console.WriteLine("日志写入完成,请检查logs目录下的日志文件。");
|
||||
|
||||
// 检查日志目录
|
||||
if (Directory.Exists("logs"))
|
||||
{
|
||||
Console.WriteLine("\n日志目录已创建,包含以下文件:");
|
||||
var logFiles = Directory.GetFiles("logs");
|
||||
foreach (var file in logFiles)
|
||||
{
|
||||
Console.WriteLine($"- {Path.GetFileName(file)}");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine("\n日志目录尚未创建,请稍候再试。");
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"配置错误: {ex.Message}");
|
||||
Console.WriteLine(ex.StackTrace);
|
||||
}
|
||||
finally
|
||||
{
|
||||
Log.CloseAndFlush();
|
||||
}
|
||||
|
||||
Console.WriteLine("\n按任意键退出...");
|
||||
Console.ReadKey();
|
||||
}
|
||||
}
|
||||
}
|
||||
25
test/LogConfigTest/appsettings.json
Normal file
25
test/LogConfigTest/appsettings.json
Normal file
@@ -0,0 +1,25 @@
|
||||
{
|
||||
"Serilog": {
|
||||
"MinimumLevel": {
|
||||
"Default": "Information",
|
||||
"Override": {
|
||||
"Microsoft": "Warning",
|
||||
"System": "Warning"
|
||||
}
|
||||
},
|
||||
"WriteTo": [
|
||||
{
|
||||
"Name": "File",
|
||||
"Args": {
|
||||
"path": "logs/api_log-.txt",
|
||||
"rollingInterval": "Day",
|
||||
"fileSizeLimitBytes": 10485760,
|
||||
"rollOnFileSizeLimit": true,
|
||||
"retainedFileCountLimit": 30,
|
||||
"shared": true,
|
||||
"outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] {SourceContext}: {Message}{NewLine}{Exception}"
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user