當(dāng)前位置:首頁 > IT技術(shù) > Web編程 > 正文

.Net core 基礎(chǔ) 創(chuàng)建及Nlog
2021-10-13 21:34:37

一、.Net Core 依賴注入

  1. .net core3.1 之前的版本需要手動(dòng)配置swagger。當(dāng)前項(xiàng)目.net core5.0版本自動(dòng)配置完成

  2. 跨域配置

    1.下載依賴包

    2.

 ? ? ? ? ? ?  //跨域
? ? ? ? ? ? ? services.AddCors(options =>
? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? options.AddDefaultPolicy(c =>
? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? //AllowAnyOrigin 來源 AllowAnyMethod 方法 AllowAnyHeader頭部信息
? ? ? ? ? ? ? ? ? ? ? c.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader();

? }); ? });

3.使用跨域 在路由之后 授權(quán)之前

 ? ? ? ? ? ?  //路由
? ? ? ? ? ? ? app.UseRouting();
? ? ? ? ? ? ? //使用跨域?。。?!
? ? ? ? ? ? ? app.UseCors();
? ? ? ? ? ? ? //授權(quán)
? ? ? ? ? ? ? app.UseAuthorization();

?

?

  1. 遷移命令:

    1.add-migration inir

    2.update-database

  2. 不使用駝峰命名規(guī)范

    services.AddControllers().AddJsonOptions(options => options.JsonSerializerOptions.PropertyNamingPolicy = null);

  3. NLog配置

    安裝nlog包image-20211013103436921

    2、在項(xiàng)目根部創(chuàng)建 nlog.config(全部小寫)文件。

    3、使用實(shí)例

    <?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"
    ? ? internalLogFile="c: empinternal-nlog-AspNetCore.txt">
    ?
    <!-- enable asp.net core layout renderers -->
    <extensions>
    ? <add assembly="NLog.Web.AspNetCore"/>
    </extensions>
    ?
    <!-- the targets to write to -->
    <targets>
    ? <!-- File Target for all log messages with basic details -->
    ? <target xsi:type="File" name="allfile" fileName="c: emp log-AspNetCore-all-${shortdate}.log"
    ? ? ? ? ? layout="${longdate}|${event-properties:item=EventId_Id:whenEmpty=0}|${uppercase:${level}}|${logger}|${message} ${exception:format=tostring}" />
    ?
    ? <!-- File Target for own log messages with extra web details using some ASP.NET core renderers -->
    ? <target xsi:type="File" name="ownFile-web" fileName="c: emp log-AspNetCore-own-${shortdate}.log"
    ? ? ? ? ? layout="${longdate}|${event-properties:item=EventId_Id:whenEmpty=0}|${uppercase:${level}}|${logger}|${message} ${exception:format=tostring}|url: ${aspnet-request-url}|action: ${aspnet-mvc-action}|${callsite}| body: ${aspnet-request-posted-body}" />
    ?
    ? <!--Console Target for hosting lifetime messages to improve Docker / Visual Studio startup detection -->
    ? <target xsi:type="Console" name="lifetimeConsole" layout="${level:truncate=4:lowercase=true}: ${logger}[0]${newline} ? ? ${message}${exception:format=tostring}" />
    </targets>
    ?
    <!-- rules to map from logger name to target -->
    <rules>
    ? <!--All logs, including from Microsoft-->
    ? <logger name="*" minlevel="Trace" writeTo="allfile" />
    ?
    ? <!--Output hosting lifetime messages to console target for faster startup detection -->
    ? <logger name="Microsoft.Hosting.Lifetime" minlevel="Info" writeTo="lifetimeConsole, ownFile-web" final="true" />
    ?
    ? <!--Skip non-critical Microsoft logs and so log only own logs (BlackHole) -->
    ? <logger name="Microsoft.*" maxlevel="Info" final="true" />
    ? <logger name="System.Net.Http.*" maxlevel="Info" final="true" />
    ? ?
    ? <logger name="*" minlevel="Trace" writeTo="ownFile-web" />
    </rules>
    </nlog>

    4、更新程序.cs 對比原代碼粘貼新代碼

    using NLog.Web;
    namespace ASP.NET_Core_5_NLog_Example
    {
    ? public class Program
    ? {
    ? ? ? public static void Main(string[] args)
    ? ? ? {
    ? ? ? ? ? var logger = NLogBuilder.ConfigureNLog("nlog.config").GetCurrentClassLogger();
    ? ? ? ?
    ? ? ? }
    ?
    ? ? ? public static IHostBuilder CreateHostBuilder(string[] args) =>
    ? ? ? ? ? Host.CreateDefaultBuilder(args)
    ? ? ? ? ? ? ? .ConfigureWebHostDefaults(webBuilder =>
    ? ? ? ? ? ? ? {
    ? ? ? ? ? ? ? ? ? webBuilder.UseStartup<Startup>();
    ? ? ? ? ? ? ? })
    ? ? ? ? ? ? ? //?。。。。?!復(fù)制以下
    ? ? ? ? ? ? ? .ConfigureLogging(logging =>
    ? ? ? ? ? ? ? {
    ? ? ? ? ? ? ? ? ? logging.ClearProviders();
    ? ? ? ? ? ? ? ? ? logging.SetMinimumLevel(LogLevel.Trace);
    ? ? ? ? ? ? ? })
    ? ? ? ? ? ? ? .UseNLog(); // NLog: Setup NLog for Dependency injection
    ? }
    }
    ?

    5、配置應(yīng)用程序安裝. json

    {
    "Logging": {
    ? "LogLevel": {
    ? ///修改Default 為 Trace
    ? ? "Default": "Trace",
    ? ? "Microsoft": "Warning",
    ? ? "Microsoft.Hosting.Lifetime": "Information"
    ? }
    },
    "AllowedHosts": "*"
    }

    6、 寫日志 將 ILogger 注入控制器:

    using Microsoft.Extensions.Logging;
    ?
    public class HomeController : Controller
    {
    ? private readonly ILogger<HomeController> _logger;
    ?
    ? public HomeController(ILogger<HomeController> logger)
    ? {
    ? ? ? _logger = logger;
    ?
    ? }
    ?
    ? public IActionResult Index()
    ? {
    ? ? ? _logger.LogInformation("測試");
    ? ? ? return View();
    ? ?

    ?

二、EF Core

  1. .net core使用EF core如下

    ?

    ?

  2. 引用依賴包 using Microsoft.EntityFrameworkCore;

    當(dāng)前類繼承DBContext 創(chuàng)建上下文

    ?

    ?

    添加上下文

    ?

    ?

    連接數(shù)據(jù)庫

    ?

    ?

  3. ?

本文摘自 :https://www.cnblogs.com/

開通會員,享受整站包年服務(wù)立即開通 >