asp.net core 多端口监听&日志服务
2017-08-05 17:42
881 查看
1 配置多个端口监听
HostingAbstractionsWebHostBuilderExtensions.
public static IWebHostBuilder UseUrls(this IWebHostBuilder hostBuilder, params string[] urls);
运行结果:
2 使用配置把多端口放到配置文件中
添加配置文件
hosting.json
{
"server.urls": "http://localhost:6001;http://localhost:5000"
}
3 .net Core 日志服务
Telemetry 日志服务
只要有访问就会记录下日志
http://localhost:6001/
Log 日志
Application Insights Telemetry (unconfigured): {"name":"Microsoft.ApplicationInsights.Dev.Request","time":"2017-08-05T09:11:22.6148689Z","tags":{"ai.device.roleInstance":"lihongbo-pc","ai.internal.sdkVersion":"aspnet5c:1.0.0","ai.operation.name":"GET /","ai.operation.id":"oUbk/2mD5Ns=","ai.user.userAgent":"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.78 Safari/537.36"},"data":{"baseType":"RequestData","baseData":{"ver":2,"id":"oUbk/2mD5Ns=","name":"GET /","startTime":"2017-08-05T09:11:22.6148689+00:00","duration":"00:00:00.1031536","success":true,"responseCode":"200","url":"http://localhost:6001/","httpMethod":"GET","properties":{"DeveloperMode":"true"}}}}
Microsoft.AspNetCore.Hosting.Internal.WebHost:Information: Request finished in 343.1286ms 200 text/html; charset=utf-8
=
关闭 Telemetry 日志服务
http://localhost:6001/
自带的Log服务
Microsoft.AspNetCore.Hosting.Internal.WebHost:Information: Request starting HTTP/1.1 GET http://localhost:6001/
Microsoft.AspNetCore.Hosting.Internal.WebHost:Information: Request finished in 8.2399ms 200 text/html; charset=utf-8
Microsoft.AspNetCore.Hosting.Internal.WebHost:Information: Request starting HTTP/1.1 GET http://localhost:6001/
Microsoft.AspNetCore.Hosting.Internal.WebHost:Information: Request finished in 4.3987ms 200 text/html; charset=utf-8
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
HostingAbstractionsWebHostBuilderExtensions.
public static IWebHostBuilder UseUrls(this IWebHostBuilder hostBuilder, params string[] urls);
运行结果:
2 使用配置把多端口放到配置文件中
添加配置文件
hosting.json
{
"server.urls": "http://localhost:6001;http://localhost:5000"
}
读取配置 var config = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile("hosting.json", optional:true) .Build(); 设置配置: var host = new WebHostBuilder() .UseKestrel() .UseContentRoot(Directory.GetCurrentDirectory()) .UseIISIntegration() //可以设置多个监听端口,http://localhost:5000/ http://localhost:6001/ 都是可以访问的 //.UseUrls("http://*:5000","http://*:6001") //添加的多端口设置放到配置中 .UseConfiguration(config) .UseStartup < Startup > () .Build(); host.Run();
3 .net Core 日志服务
Telemetry 日志服务
public void ConfigureServices(IServiceCollection services) { // Add framework services. services.AddApplicationInsightsTelemetry(Configuration); } public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) { loggerFactory.AddConsole(Configuration.GetSection("Logging")); loggerFactory.AddDebug(); app.UseApplicationInsightsRequestTelemetry(); }
只要有访问就会记录下日志
http://localhost:6001/
Log 日志
Application Insights Telemetry (unconfigured): {"name":"Microsoft.ApplicationInsights.Dev.Request","time":"2017-08-05T09:11:22.6148689Z","tags":{"ai.device.roleInstance":"lihongbo-pc","ai.internal.sdkVersion":"aspnet5c:1.0.0","ai.operation.name":"GET /","ai.operation.id":"oUbk/2mD5Ns=","ai.user.userAgent":"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.78 Safari/537.36"},"data":{"baseType":"RequestData","baseData":{"ver":2,"id":"oUbk/2mD5Ns=","name":"GET /","startTime":"2017-08-05T09:11:22.6148689+00:00","duration":"00:00:00.1031536","success":true,"responseCode":"200","url":"http://localhost:6001/","httpMethod":"GET","properties":{"DeveloperMode":"true"}}}}
Microsoft.AspNetCore.Hosting.Internal.WebHost:Information: Request finished in 343.1286ms 200 text/html; charset=utf-8
=
关闭 Telemetry 日志服务
http://localhost:6001/
自带的Log服务
Microsoft.AspNetCore.Hosting.Internal.WebHost:Information: Request starting HTTP/1.1 GET http://localhost:6001/
Microsoft.AspNetCore.Hosting.Internal.WebHost:Information: Request finished in 8.2399ms 200 text/html; charset=utf-8
Microsoft.AspNetCore.Hosting.Internal.WebHost:Information: Request starting HTTP/1.1 GET http://localhost:6001/
Microsoft.AspNetCore.Hosting.Internal.WebHost:Information: Request finished in 4.3987ms 200 text/html; charset=utf-8
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
//配置ConsoleLog服务 loggerFactory.AddConsole(Configuration.GetSection("Logging")); //设置服务输出级别 //Debug 级别会输出包括Information级别的所有日志 loggerFactory.AddDebug(); }
相关文章推荐
- Docker & Consul & Fabio & ASP.NET Core 2.0 微服务跨平台实践
- 把Asp.Net Core 2.0部署在Linux上,使用Nginx代理服务器,并且用Systemctl命令以服务的方式监听项目
- Docker & ASP.NET Core 2.0 微服务跨平台实践
- 【ASP.NET Core】依赖注入高级玩法——如何注入多个服务实现类
- 解读ASP.NET 5 & MVC6系列(9):日志框架
- ASP.NET Core 2.0 使用NLog实现日志记录
- ASP.NET Core应用中如何记录和查看日志
- 在ASP.NET Core Web API中为RESTful服务增加对HAL的支持
- Ubuntu & GitLab CI & Docker & ASP.NET Core 2.0 自动化发布和部署(2)
- (6)学习笔记 ) ASP.NET CORE微服务 Micro-Service ---- AOP框架
- ASP.NET Core 添加日志NLog
- 我眼中的ASP.NET Core之微服务 (二)
- asp.net core2.0 JWT Bearer 注意添加认证服务
- Asp.Net Core中服务的生命周期选项区别和用法
- ASP.NET Core应用中如何记录和查看日志
- (8)学习笔记 ) ASP.NET CORE微服务 Micro-Service ---- Ocelot网关(Api GateWay)
- Centos7 &amp; Docker &amp; Jenkins &amp; ASP.NET Core
- 在ASP.NET Core中使用Apworks开发数据服务:对HAL的支持
- ASP.NET Core "完整发布,自带运行时" 到jexus