.NET Core 問題記錄

前言:

 最近在項目中遇到了遇到了寫部署步驟過多的問題,為了減少.net core項目部署步驟;需要對一些基礎問題進行驗證:

 如埠設置、單頁應用程式(angluar)合併部署方式等相關問題,特將解決過程記錄下來

一、.NET Core部署埠指定問題?

 Kestrel 是 ASP.NET Core 項目模板指定的默認 Web 伺服器。

 那麼在.NET Core中以Kestrel 作為作為web伺服器有哪些方式能指定服務的監聽埠呢?

  • 方式1:環境變數設置:launchSettings.json文件中指定applicationUrl地址

   修改launchSettings.json的配置項applicationUrl值:如下兩種設置方式;多個地址用;分割

    

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

 運行結果:

  

  • 方式2:程式碼中設置地址

   在ConfigureWebHostDefaults中設置啟動默認值時,使用UseUrls綁定地址;多個地址用;分割

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

    運行如下:

    

    此時:方式1的設置依然存在,說明優先順序:方式2>方式1     

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

    修改程式配置文件:添加以下節點:

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

   運行效果:

    

    此時:方式1、方式2的設置依然存在,說明優先順序:方式3>方式2>方式1

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

   使用以下命令啟動程式:

//項目根目錄運行:
dotnet run --urls "//*:5300;//*:5301"

//編譯輸出命令運行
dotnet AuditLogDemo.dll --urls "//*:5300;//*:5301"

   運行效果:

    

  所以最後可以得出各種方式優先順序為:

   命令行配置(方式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 =>{}); } }

    運行效果:

    

三、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="//www.nlog-project.org/schemas/NLog.xsd"
      xmlns:xsi="//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、運行效果:

  

   

  更加詳細的使用方式:

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

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