1. 程式人生 > 其它 >.net core 2.2 升級 .net 6 日誌、redis、steeltoe微服務註冊

.net core 2.2 升級 .net 6 日誌、redis、steeltoe微服務註冊

一、新增nlog日誌

1.新增Nuget程式包

1.1 NLog 
1.2 NLog.Web.AspNetCore

2.建立Nlog配置檔案

2.1建立的檔名稱為 nlog.config (ps:也可以自定義其他名字,載入配置檔案時修改為自定義的名稱即可)
2.2 下面配置檔案打印出來的效果如下圖
![在這裡插入圖片描述](https://imgconvert.csdnimg.cn/aHR0cHM6Ly9ub3RlLnlvdWRhby5jb20veXdzL3B1YmxpYy9yZXNvdXJjZS82ZjBmY2U5YTBiNTcxMGJjMTQyNTc3MjZjMzEzNTdmZC94bWxub3RlL0EzREJCNjNDNzEzNzQ3RjBBOUYxMDdGQUY0MjJGOTRGLzIyNjU?x-oss-process=image/format,png)
<?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" internalLogLevel="Info">
    <!-- 啟用.net core的核心佈局渲染器 -->
    <extensions>
        <add assembly="NLog.Web.AspNetCore"
/> </extensions> <!-- 寫入日誌的目標配置 --> <targets> <!-- 除錯 --> <target xsi:type="File" name="debug" fileName="logs/debug-${shortdate}.log" layout="${longdate}|${event-properties:item=EventId_Id}|${uppercase:${level}}|${logger}|${message} ${exception:format=tostring}|url: ${aspnet-request-url}|action: ${aspnet-mvc-action}"
/> <!-- 警告 --> <target xsi:type="File" name="warn" fileName="logs/warn-${shortdate}.log" layout="${longdate}|${event-properties:item=EventId_Id}|${uppercase:${level}}|${logger}|${message} ${exception:format=tostring}|url: ${aspnet-request-url}|action: ${aspnet-mvc-action}" /> <!-- 錯誤 --> <target xsi:type="File" name="error" fileName="logs/error-${shortdate}.log" layout="${longdate}|${event-properties:item=EventId_Id}|${uppercase:${level}}|${logger}|${message} ${exception:format=tostring}|url: ${aspnet-request-url}|action: ${aspnet-mvc-action}" /> </targets> <!-- 對映規則 --> <rules> <!-- 除錯 --> <logger name="*" minlevel="Trace" maxlevel="Debug" writeTo="debug" /> <!--跳過不重要的微軟日誌--> <logger name="Microsoft.*" maxlevel="Info" final="true" /> <!-- 警告 --> <logger name="*" minlevel="Info" maxlevel="Warn" writeTo="warn" /> <!-- 錯誤 --> <logger name="*" minlevel="Error" maxlevel="Fatal" writeTo="error" /> </rules> </nlog>

3.Program.cs 新增Nlog

public static void Main(string[] args)
        {

            CreateHostBuilder(args).Build().Run();
        }
        public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args).ConfigureWebHostDefaults(webBuilder =>
            {
                webBuilder.UseStartup<Startup>();
            })
                //配置nlog
                .ConfigureLogging(logging =>
                {
                    logging.ClearProviders();
                }).UseNLog();

 

 

二、新增redis

1.新增Nuget程式包

1.Microsoft.Extensions.Caching.StackExchangeRedis
2.StackExchange.Redis

2.配置appsettings.json

"Redis": {
"Connection": "192.168.0.6:6379,password=123456,defaultDatabase=5,poolsize=50,ssl=false,writeBuffer=10240,prefix=table:"
},

3.Startup.cs類 ConfigureServices註冊服務

services.AddStackExchangeRedisCache(options =>
            {
                options.Configuration = Configuration["Redis:Connection"];

            });

 

三、steeltoe微服務註冊(Eureka)

1.新增Nuget程式包

1.Steeltoe.Discovery.ClientCore
2.Steeltoe.Discovery.Eureka //必須得新增這個,否則無法註冊到Eureka中

2.配置檔案

{
  "Logging": {
    "IncludeScopes": false,
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information",
      "Steeltoe": "Debug"//啟動日誌
    }
  },
  "AllowedHosts": "*",
  "Urls": "http://localhost:9003",
  "spring": {
    "application": {
      "name": "app-pets-net"
    }
  },
  "eureka": {
    "client": {
      "serviceUrl": "http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka", //註冊中心url 叢集用,隔開
      "shouldFetchRegistry": true, //發現服務
      "shouldRegisterWithEureka": true, //註冊服務
      "validate_certificates": false
    },
    "instance": {
      "hostName": "127.0.0.1",
      "appName": "app-pets-net-pai", //預設是在執行時自動確認的 不加上這個的話閘道器找不到服務
      "port": 9003, //設定該服務中註冊的埠
      "leaseRenewalIntervalInSeconds": 30,
      "leaseExpirationDurationInSeconds": 90
    }
  }
}

3.Startup.cs類

 ConfigureServices註冊服務

services.AddDiscoveryClient(Configuration);

Configure 方法

app.UseDiscoveryClient();