• .NET Core自定义TagHelper和使用Serilog


    代码:

    using Microsoft.AspNetCore.Razor.TagHelpers;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Threading.Tasks;
    
    namespace NetCoreTestMVC2.Commons
    {
        //public class EmailTagHelper : TagHelper
        //{
        //  示例1:<email mail-to="xxx@qq.com"></email>
        //  public string MailTo { get; set; }
        //  public override void Process(TagHelperContext context, TagHelperOutput output)
        //  {
        //      output.TagName = "a";
        //      output.Attributes.SetAttribute("href",$"mailto:{MailTo}");
        //      output.Content.SetContent(MailTo);
        //  }
    
        //  示例2:<email>xxx@qq.com</email>
        //  public async override Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
        //  {
        //      output.TagName = "a";
        //      var content = await output.GetChildContentAsync();
        //      var target = content.GetContent();
        //      output.Attributes.SetAttribute("href", $"mailto:{target}");
        //      output.Content.SetContent(target);
        //  }
        //}
    
        //[HtmlTargetElement(Attributes = "bold")]
        //[HtmlTargetElement("bold")]
        //public class BoldTagHelper : TagHelper
        //{
        //    //示例:<p bold>xxx</p> 或者 <bold>xxx</bold> 
        //    public override void Process(TagHelperContext context, TagHelperOutput output)
        //    {
        //        output.Attributes.RemoveAll("bold");
        //        output.PreContent.SetHtmlContent("<strong>");
        //        output.PostContent.SetHtmlContent("</strong>");
        //    }
        //}
    
        /// <summary>
        /// 条件 TagHelper
        /// <div condition="_SignInManager.IsSignIn(User)">xxx</div>
        /// 如果 condition 中返回的值是 false 则不会显示 div
        /// </summary>
        //public class ConditionTagHelper : TagHelper
        //{
        //    public bool Condition { get; set; }
        //    public override void Process(TagHelperContext context, TagHelperOutput output)
        //    {
        //        if (!Condition)
        //        {
        //            output.SuppressOutput();
        //        }
        //    }
        //}
    
        //前端使用TagHelper前缀,@tagHelperPrefix "tagpre:"
        //TagHelper 前缀,使用在标签前加上<tagpre:a>xxx</tagpre:a>
    }

     nuget使用日志Nuget安装:Serilog/Serilog.AspNetCore/Serilog.Sinks.File/Serilog.Sinks.Console
    配置:

    public class Program
        {
            public static void Main(string[] args)
            {
                Log.Logger = new LoggerConfiguration()
                    .MinimumLevel.Debug()
                    .MinimumLevel.Override("Microsoft", LogEventLevel.Information)
                    .Enrich.FromLogContext()
                    .WriteTo.Console()
                    .WriteTo.File(Path.Combine("logs", "log.txt"), rollingInterval: RollingInterval.Day)
                    .CreateLogger()
                    ;
                CreateWebHostBuilder(args).Build().Run();
            }
    
            public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
                WebHost.CreateDefaultBuilder(args)
                .UseSerilog()
                .UseStartup<Startup>();
        }
  • 相关阅读:
    添加到path的作用
    java post格式发送application/x-www-form-urlencoded
    c# 获取本机ip
    Winform学习(八)——使用setup打包程序
    kubernetes 核心技术-ingress
    ExceptionHandler的执行顺序
    cookie、session、token、jwt详解与sso基本实现原理
    java函数式编程及JDK常用函数式接口
    vue饼图
    vue动态页签
  • 原文地址:https://www.cnblogs.com/xsj1989/p/13851473.html
Copyright © 2020-2023  润新知