日志级别学习
// Trace:0,包含最详细的消息。 这些消息可能包含敏感的应用数据。
// Debug:1,用于调试和开发。
// Information:2,一般用于跟踪系统的日志。
// Warning:3,一般用于记录异常事件或意外事件。
// Error:4,一般用于记录无法处理的异常。
// Critical:5,需要马上处理的失败,比如数据丢失、磁盘控件不足。
// None:6,关闭日志打印。
// System.Console.WriteLine("get html by url");
_logger.LogTrace("Trace");
_logger.LogDebug("Debug");
_logger.LogInformation("Information");
_logger.LogWarning("Warning");
_logger.LogError("Error");
_logger.LogCritical("Critical");
Wechat.CrawlerNew/Wechat.App/Program.cs
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Wechat.Service.Browser;
using Wechat.Service.Topic;
// get settings from appsettings.json
var config = new ConfigurationBuilder()
.SetBasePath(AppContext.BaseDirectory)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.Build();
HostApplicationBuilder builder = Host.CreateApplicationBuilder(args);
// add logger
builder.Services.AddLogging(builder =>
{
builder.AddConfiguration(config.GetSection("Logging"));
builder.AddConsole();
});
builder.Services.AddSingleton<ITopicService, TopicService>();
builder.Services.AddSingleton<IBrowserService, BrowserService>();
using IHost host = builder.Build();
// get service from IServiceProvider
var topicService = host.Services.GetRequiredService<ITopicService>();
topicService.ExtractLinksFromUrl("World");
// host.Run();
// await host.RunAsync();
// host.Dispose();
Wechat.CrawlerNew/Wechat.App/appsettings.Development.json
{
"Logging": {
"LogLevel": {
"Default": "Debug",
"Microsoft.AspNetCore": "Warning"
}
}
}
Wechat.CrawlerNew/Wechat.App/Wechat.App.csproj
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\Wechat.Entity\Wechat.Entity.csproj" />
<ProjectReference Include="..\Wechat.Service\Wechat.Service.csproj" />
</ItemGroup>
<ItemGroup>
<None Update="nlog.config" CopyToOutputDirectory="Always" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="AngleSharp" Version="1.2.0-beta.410" />
<PackageReference Include="HtmlAgilityPack" Version="1.11.60" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="8.0.0" />
<PackageReference Include="NLog.Extensions.Logging" Version="5.3.8" />
<PackageReference Include="Selenium.Support" Version="4.18.1" />
<PackageReference Include="Selenium.WebDriver" Version="4.18.1" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="8.0.0" />
</ItemGroup>
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>
Wechat.CrawlerNew/Wechat.App/appsettings.json
{
"ConnectionStrings": {
"MsSqlConnection": "Server=(localdb)\\mssqllocaldb;Database=Electric;Trusted_Connection=True;MultipleActiveResultSets=true",
"MySqlConnection": "Server=localhost;Database=Electric;Uid=root;Pwd=123456;"
},
"DataProvider": "MsSql",
"Logging": {
"LogLevel": {
"Default": "Debug",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*",
"Authentication": {
"JwtBearer": {
"SecurityKey": "ELETRICAPIC421AAEE0D114E9C",
"Issuer": "EletricAPI",
"Audience": "EletricAPI"
}
},
"CorsOrigins": "http://localhost:9527"
}
Wechat.CrawlerNew/Wechat.Service/Browser/BrowserService.cs
using Microsoft.Extensions.Logging;
namespace Wechat.Service.Browser;
public class BrowserService : IBrowserService
{
private readonly ILogger<BrowserService> _logger;
public BrowserService(ILogger<BrowserService> logger)
{
_logger = logger;
}
public string GetHtmlByUrl(string url)
{
// Trace:0,包含最详细的消息。 这些消息可能包含敏感的应用数据。
// Debug:1,用于调试和开发。
// Information:2,一般用于跟踪系统的日志。
// Warning:3,一般用于记录异常事件或意外事件。
// Error:4,一般用于记录无法处理的异常。
// Critical:5,需要马上处理的失败,比如数据丢失、磁盘控件不足。
// None:6,关闭日志打印。
// System.Console.WriteLine("get html by url");
_logger.LogTrace("Trace");
_logger.LogDebug("Debug");
_logger.LogInformation("Information");
_logger.LogWarning("Warning");
_logger.LogError("Error");
_logger.LogCritical("Critical");
return string.Empty;
}
}
Wechat.CrawlerNew/Wechat.Service/Browser/IBrowserService.cs
namespace Wechat.Service.Browser;
public interface IBrowserService{
string GetHtmlByUrl(string url);
}
Wechat.CrawlerNew/Wechat.Service/Topic/TopicService.cs
using Wechat.Service.Browser;
namespace Wechat.Service.Topic;
public class TopicService : ITopicService
{
private readonly IBrowserService _browserService;
public TopicService(IBrowserService browserService)
{
_browserService = browserService;
}
public List<string> ExtractLinksFromUrl(string url)
{
_browserService.GetHtmlByUrl(url);
return new List<string>{ "" };
}
}
Wechat.CrawlerNew/Wechat.Service/Topic/ITopicService.cs
namespace Wechat.Service.Topic;
public interface ITopicService
{
List<string> ExtractLinksFromUrl(string url);
}
Wechat.CrawlerNew/Wechat.Service/Wechat.Service.csproj
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="8.0.0" />
</ItemGroup>
</Project>
标签:控制台,Service,配置文件,public,Wechat,CrawlerNew,using,日志,logger
From: https://www.cnblogs.com/zhuoss/p/18181161