初学ASP.NET Core Razor 编程

    离上次写博客已经过去好几年了,那是因为没有更新学习了,玩了几年。哈哈哈

    耍安逸了,现在准备又开始刚毕业时的那种学习和工作状态,也给自己列了学习计划。希望用3个月时间好好学习下core并做几个小项目。

    先从初级入门,参照DotNet菜园 大神的教程自己写了一遍,地址https://www.cnblogs.com/chillsrc/p/9517172.html 感兴趣的可以看看。

    代码我就不贴了,主要说一说,自己的改进和总结吧

    一.读取json配置文件

     在项目中,我们可能会经常从配置文件中读取配置,所以我自己写了一个配置文件帮助类,读取配置文件有三种方式,我用了一下我认为最简单的一种

 1  public class JsonConfigurationHelper
 2     {
 3         /// <summary>
 4         ///  简单的读取json 文件中的数据 "班级编号=" + config["ClassNum"] + ",班级名称=" + config["ClassName"] + "\r"
 5         ///       + "学生名称=" + config["Students:0:name"] + ",学生年纪=" + config["Students:0:age"]
 6         /// </summary>
 7         /// <param name="key"></param>
 8         /// <param name="path"></param>
 9         /// <returns></returns>
10         public static string GetAppSettingsWay1(string key, string path = "appsettings.json")
11         {
12             var builder = new ConfigurationBuilder().AddJsonFile(path);
13             var config = builder.Build();
14             return config[key];
15         }
16 }

      二.日志文件,

       我这个地方用的log4,首先是配置文件log4net.config

 1 <?xml version="1.0" encoding="utf-8" ?>
 2 
 3 <configuration>
 4 
 5     <!-- This section contains the log4net configuration settings -->
 6 
 7     <log4net>
 8 
 9         <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
10 
11             <file value="Log/" />
12 
13             <appendToFile value="true" />
14 
15             <rollingStyle value="Composite" />
16 
17             <staticLogFileName value="false" />
18 
19             <datePattern value="yyyyMMdd'.log'" />
20 
21             <maxSizeRollBackups value="10" />
22 
23             <maximumFileSize value="50MB" />
24 
25             <layout type="log4net.Layout.PatternLayout">
26 
27                 <conversionPattern value="%date  [%thread]  %-5level  %message%newline" />
28 
29             </layout>
30 
31         </appender>
32 
33 
34 
35         <!-- Setup the root category, add the appenders and set the default level -->
36 
37         <root>
38 
39             <level value="ALL" />
40 
41             <appender-ref ref="RollingLogFileAppender" />
42 
43         </root>
44 
45 
46 
47     </log4net>
48 
49 </configuration>

      日志文件帮助类

  1   public class Logger
  2     {
  3         private static ILog logger;
  4 
  5         static Logger()
  6 
  7         {
  8 
  9             if (logger == null)
 10 
 11             {
 12 
 13                 var repository = LogManager.CreateRepository("NETCoreRepository");
 14 
 15                 //log4net从log4net.config文件中读取配置信息
 16 
 17                 XmlConfigurator.Configure(repository, new FileInfo("log4net.config"));
 18 
 19                 logger = LogManager.GetLogger(repository.Name, "InfoLogger");
 20 
 21             }
 22 
 23         }
 24 
 25 
 26 
 27         /// <summary>
 28 
 29         /// 普通日志
 30 
 31         /// </summary>
 32 
 33         /// <param name="message"></param>
 34 
 35         /// <param name="exception"></param>
 36 
 37         public static void Info(string message, Exception exception = null)
 38 
 39         {
 40 
 41             if (exception == null)
 42 
 43                 logger.Info(message);
 44 
 45             else
 46 
 47                 logger.Info(message, exception);
 48 
 49         }
 50 
 51 
 52 
 53         /// <summary>
 54 
 55         /// 告警日志
 56 
 57         /// </summary>
 58 
 59         /// <param name="message"></param>
 60 
 61         /// <param name="exception"></param>
 62 
 63         public static void Warn(string message, Exception exception = null)
 64 
 65         {
 66 
 67             if (exception == null)
 68 
 69                 logger.Warn(message);
 70 
 71             else
 72 
 73                 logger.Warn(message, exception);
 74 
 75         }
 76 
 77 
 78 
 79         /// <summary>
 80 
 81         /// 错误日志
 82 
 83         /// </summary>
 84 
 85         /// <param name="message"></param>
 86 
 87         /// <param name="exception"></param>
 88 
 89         public static void Error(string message, Exception exception = null)
 90 
 91         {
 92 
 93             if (exception == null)
 94 
 95                 logger.Error(message);
 96 
 97             else
 98 
 99                 logger.Error(message, exception);
100 
101         }
102 
103     }

        日志文件的调用方法   Logger.Info("connectionString");

        一周时间就学习了这么多。感触颇多,asp.netcore 和asp.net 还有mvc比较起来少了好多东西刚开始真心不适应,比如以前的global 文件web.config

      这些都没有了还得要继续学习才行

  

    

posted @ 2018-12-13 15:55  mrzhongwei  阅读(188)  评论(0编辑  收藏  举报