ASP.NET Core 可以很轻松的在模板上依赖注入日志模块 ILogger,但是对于控制台应用(Console App)ILogger 就不是那么友好,想要在 Console App 使用日志模块,需要手动构建依赖注入环境

环境

  • Dotnet Core 3.1
  • VS2019

参考

步骤

添加 Nuget 包

在项目内添加包:

  • Microsoft.Extensions.DependencyInjection
  • Microsoft.Extensions.Logging
  • Microsoft.Extensions.Logging.Console

配置 ILogger 模块的依赖注入

以 Dotnet Core Console App 模板为例,代码如下

using System;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Linq;
using System.Collections.Generic;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;

namespace ILogger-Test{
    class Program {
        #region DI variables
        private readonly ILogger _logger;
        #endregion

        public Program(ILogger<Program> logger) {
            _logger = logger;
        }

        private static void ConfigureServices(ServiceCollection services) {
            services.AddLogging(logging => {
                //logging.AddFilter("Program", LogLevel.Debug);
                logging.AddFilter("System", LogLevel.Warning);
                logging.AddFilter("Microsoft", LogLevel.Warning);
                logging.AddConsole();
            }).AddTransient<Program>();
        }
        
        internal async Task Run() {
            _logger.LogInformation(DateTime.Now.ToString() + " - Start Job -");
            
            //...

            _logger.LogInformation(DateTime.Now.ToString() + " - End Job -");
        }

        static async Task Main() {
            #region config service
            var services = new ServiceCollection();
            ConfigureServices(services);
            using (ServiceProvider serviceProvider = services.BuildServiceProvider()) {
                Program app = serviceProvider.GetService<Program>();
                await app.Run();
            }
            #endregion
        }
    }
}

Continue reading “Dotnet Core Console App 使用 ILogger 依赖注入”

仅作记录,使用 EPPlus 组件,文字、图片、代码均来自参考链接

参考

示例

EPPlus 官方示例

In most cases you probably have some data that you want to move to an Excel spreadsheet, do some styling, maybe add a formula or a chart.

But before we get started, here’s something to keep in mind when you work with EPPlus:
Cell addresses, number formats and formulas are culture-insensitive, meaning things might look a little bit different when you write your code. This is the way OOXML is stored and is then translated too your culture when the workbook is opened in Excel.

Addresses are separated by a comma (,).
Example worksheet.Cells["A1:C1,C3"].Style.Font.Bold = true.

Numberformats use dot for decimal (.) and comma (,) for thousand separator.
Example worksheet.Cells["B2:B3"].Style.NumberFormat.Format = "#,##0.00";.

Formulas use comma (,) to separate parameters.
Example worksheet.Cells["C11"].Formula="SUBTOTAL(9,\"C1:C10\")";.

The first thing you do is to create an instance to the ExcelPackage class. To do that you first need to add a using directive to OfficeOpenXml namespace in the top of your file. This is the top namespace in EPPlus;

using OfficeOpenXml;

You can now reference the Excelpackage class directly for your class. The ExcelPackage class has few different constructors depending on what you want to do…

Continue reading “.NET Core 导入导出 Excel”