• 欢迎访问开心洋葱网站,在线教程,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站,欢迎加入开心洋葱 QQ群
  • 为方便开心洋葱网用户,开心洋葱官网已经开启复制功能!
  • 欢迎访问开心洋葱网站,手机也能访问哦~欢迎加入开心洋葱多维思维学习平台 QQ群
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏开心洋葱吧~~~~~~~~~~~~~!
  • 由于近期流量激增,小站的ECS没能经的起亲们的访问,本站依然没有盈利,如果各位看如果觉着文字不错,还请看官给小站打个赏~~~~~~~~~~~~~!

.NET Core 问题记录

C# chaney1992 1877次浏览 0个评论

前言:

 最近在项目中遇到了遇到了写部署步骤过多的问题,为了减少.net core项目部署步骤;需要对一些基础问题进行验证:

 如端口设置、单页应用程序(angluar)合并部署方式等相关问题,特将解决过程记录下来

一、.NET Core部署端口指定问题?

 Kestrel 是 ASP.NET Core 项目模板指定的默认 Web 服务器。

 那么在.NET Core中以Kestrel 作为作为web服务器有哪些方式能指定服务的监听端口呢?

  • 方式1:环境变量设置:launchSettings.json文件中指定applicationUrl地址

   修改launchSettings.json的配置项applicationUrl值:如下两种设置方式;多个地址用;分割

    .NET Core 问题记录

{
  "$schema": "http://json.schemastore.org/launchsettings.json","profiles": {
    "AuditLogDemo": {
      "commandName": "Project",
      "launchBrowser": true,
      "launchUrl": "swagger",
      "applicationUrl": "http://localhost:5000;https://localhost:5001",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
  }
}

 运行结果:

  .NET Core 问题记录

  • 方式2:代码中设置地址

   在ConfigureWebHostDefaults中设置启动默认值时,使用UseUrls绑定地址;多个地址用;分割

public static IHostBuilder CreateHostBuilder(string[] args) =>
  Host.CreateDefaultBuilder(args)
    //改用Autofac来实现依赖注入
    .UseServiceProviderFactory(new AutofacServiceProviderFactory())
    .ConfigureWebHostDefaults(webBuilder =>
    {
      webBuilder.UseStartup<Startup>();
      webBuilder.UseUrls("http://*:5100;http://*:5101");
    });

    运行如下:

    .NET Core 问题记录

    此时:方式1的设置依然存在,说明优先级:方式2>方式1     

  • 方式3:配置文件设置(appsettings.json)-推荐

    修改程序配置文件:添加以下节点:

{
    "Urls": "http://*:5200;https://*:5201"  
}

   运行效果:

    .NET Core 问题记录

    此时:方式1、方式2的设置依然存在,说明优先级:方式3>方式2>方式1

  • 方式4:使用命令行配置

   使用以下命令启动程序:

//项目根目录运行:
dotnet run --urls "http://*:5300;https://*:5301"

//编译输出命令运行
dotnet AuditLogDemo.dll --urls "http://*:5300;https://*:5301"

   运行效果:

    .NET Core 问题记录

  所以最后可以得出各种方式优先级为:

   命令行配置(方式4)>配置文件方式(方式3)>程序指定(方式2)>环境变量配置(方式1)

二、Angular(单页应用程序)开发页面采用Kestrel 服务器运行

   由于项目前期采用前后端分离实现,但在实施部署环节需要分成两个站点;给实施人员带来了多余的步骤。那么怎么解决这个问题呢?

  1、添加包引用:Microsoft.AspNetCore.SpaServices.Extensions

Install-Package Microsoft.AspNetCore.SpaServices.Extensions

  2、修改Startup.cs 文件中方法:  

public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }public IConfiguration Configuration { get; }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    { services.AddSpaStaticFiles(configuration => {
       //指定单页应用文件路径地址 configuration.RootPath = "wwwroot/dist"
; });
     //…… } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { app.UseStaticFiles();
     // app.UseSpaStaticFiles(); app.UseEndpoints(endpoints => { endpoints.MapControllers();
       //绑定路由;必须设置 endpoints.MapControllerRoute(name:
"default", pattern: "{controller}/{action=Index}/{id?}"); }); app.UseSpa(configuration =>{}); } }

    运行效果:

    .NET Core 问题记录

三、LogDashboard的使用

 在项目中查看日志一直都是直接查看日志文件,那么有没有办法直接在页面中查看日志内容呢?

 最近了解到一个开源项目:LogDashboard 采用中间件方式,提供了一个可以简单快速查看日志的面板。使用简单方便。 

 使用方式:

  1、添加包引用:LogDashboard

Install-Package LogDashboard

  2、在Startup中使用LogDashboard

public void ConfigureServices(IServiceCollection services)
{
    services.AddLogDashboard();
}


public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    //使用日志看板
    app.UseLogDashboard();
}

  3、添加NLog及NLog配置文件   

<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      autoReload="true"
      throwExceptions="false"
      internalLogLevel="Off" internalLogFile="c:\temp\nlog-internal.log">
  <variable name="myvar" value="myvalue"/>
  <targets>
    <target xsi:type="file" name="File" fileName="${basedir}/logs/${shortdate}.log"
            layout="${longdate}||${level}||${logger}||${message}||${exception:format=ToString:innerFormat=ToString:maxInnerExceptionLevel=10:separator=\r\n}||end" />
  </targets>
  <rules>
    <logger name="*" minlevel="Debug" writeTo="file" />
  </rules>
</nlog>

  4、运行效果:

  .NET Core 问题记录

   .NET Core 问题记录

  更加详细的使用方式:

   https://doc.logdashboard.net/ru-men/quickstart

其他:
 本篇文章示例源码:https://github.com/cwsheng/AuditLogDemo


开心洋葱 , 版权所有丨如未注明 , 均为原创丨未经授权请勿修改 , 转载请注明.NET Core 问题记录
喜欢 (0)

您必须 登录 才能发表评论!

加载中……