備忘録

備忘録

ConfigurationBuilderを利用して設定ファイルを読み込む方法

Ⅰ. はじめに

タイトルの通り「ConfigurationBuilderを利用して設定ファイルを読み込む方法」です。

Ⅱ. やり方

1. 必要なパッケージをインストールする
Install-Package Microsoft.Extensions.Hosting
Install-Package Microsoft.Extensions.Configuration
2. サンプルプログラムを書く

appsettings.json

{
  "Key001": "value001",
  "Key002": 123,
  "Key003": [ "value001", "value002", "value003" ],
  "Key004": {
    "SubKey001": "sub_value001"
  },
  "Key005": [
    { "Id": 1, "Name": "name001", "CreatedAt": "2020/01/01 00:00" },
    { "Id": 2, "Name": "name002", "CreatedAt": "2020/01/02 00:00" },
    { "Id": 3, "Name": "name003", "CreatedAt": "2020/01/03 00:00" }
  ]
}

Program.cs

using Microsoft.Extensions.Configuration;
using System;
using System.IO;

namespace Test
{
  class User
  {
    public string Name { get; set; }
    public int Id { get; set; }
    public DateTime CreatedAt { get; set; }
  }

  class Program
  {
    static void Main(string[] args)
    {
      var config = new ConfigurationBuilder()
        .SetBasePath(Directory.GetCurrentDirectory())
        .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
        .Build();

      Console.WriteLine(config.GetValue<string>("Key001"));
      Console.WriteLine(config.GetValue<int>("Key002"));
      Console.WriteLine(string.Join(",", config.GetSection("Key003").Get<string[]>()));
      Console.WriteLine(config.GetValue<string>("Key004:SubKey001"));

      /*
      var users = config.GetSection("Key005").GetChildren().Select(x => new User
      {
        Id = x.GetValue<int>("Id"),
        Name = x.GetValue<string>("Name"),
        CreatedAt = x.GetValue<DateTime>("CreatedAt")
      }).ToArray();
      */

      var users = config.GetSection("Key005").Get<User[]>();
      foreach (var user in users)
      {
        Console.WriteLine($"Id: {user.Id}, Name: {user.Name}, CreatedAt: {user.CreatedAt}");
      }
    }
  }
}

実行結果

value001
123
value001,value002,value003
sub_value001
Id: 1, Name: name001, CreatedAt: 2020/01/01 0:00:00
Id: 2, Name: name002, CreatedAt: 2020/01/02 0:00:00
Id: 3, Name: name003, CreatedAt: 2020/01/03 0:00:00

NuxtJSでFont Awesomeを利用する方法

Ⅰ. はじめに

タイトルの通り「NuxtJSでFont Awesomeを利用する方法」です。

Ⅱ. やり方

1. 必要なパッケージをインストールする
npm i -D @nuxtjs/fontawesome
npm i -D @fortawesome/free-solid-svg-icons
2. ファイルを編集する

nuxt.config.js

buildModules: [
  ['@nuxtjs/fontawesome', {
    component: 'fa',
    suffix: true,
    icons: {
      solid: ['faThumbsUp']
    }
  }]
]

pages/index.vue

<template>
  <fa-icon icon="thumbs-up" />
</template>

実行結果


コンソールアプリケーションでGeneric Hostを利用する方法

Ⅰ. はじめに

タイトルの通り「コンソールアプリケーションでGeneric Hostを利用する方法」です。

Ⅱ. やり方

1. 必要なパッケージをインストールする
Install-Package Microsoft.Extensions.Hosting
2. サンプルプログラムを書く
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

var builder = new HostBuilder()
   .ConfigureServices((hostContext, services) =>
   {
     services.Configure<HostOptions>(options =>
     {
       // options.ServicesStartConcurrently = true; // .NET 8 以降
       // options.ServicesStopConcurrently = true; // .NET 8 以降
     });
     services.AddHostedService<MyService>();
   });
await builder.RunConsoleAsync();

public class MyService : IHostedService
{
  private readonly IHostApplicationLifetime AppLifetime;

  public MyService(IHostApplicationLifetime appLifetime)
  {
    AppLifetime = appLifetime;
  }

  public Task StartAsync(CancellationToken cancellationToken)
  {
    Console.WriteLine("StartAsync");

    AppLifetime.ApplicationStarted.Register(OnStarted);
    AppLifetime.ApplicationStopping.Register(OnStopping);
    AppLifetime.ApplicationStopped.Register(OnStopped);

    return Task.CompletedTask;
  }

  public void OnStarted()
  {
    Console.WriteLine("OnStarted");
  }

  public void OnStopping()
  {
    Console.WriteLine("OnStopping");
  }

  public Task StopAsync(CancellationToken cancellationToken)
  {
    Console.WriteLine("StopAsync");
    return Task.CompletedTask;
  }

  public void OnStopped()
  {
    Console.WriteLine("OnStopped");
  }
}

実行結果

StartAsync
OnStarted

※Ctrl + Cを入力すると以下がコンソールに出力されます。

OnStopping
StopAsync
OnStopped

Detoursを利用してWin32APIの呼び出しをモニタする方法

Ⅰ. はじめに

タイトルの通り「Detoursを利用してWin32APIをモニタする方法」です。

Ⅱ. やり方

1. Detoursをビルドする

https://github.com/microsoft/Detours

2. batファイルを作成する

start.bat

start syelogd.exe /q c:\result.txt
timeout /T 1

rem notepad.exe をモニタする
withdll /d:trcapi64.dll C:\Windows\System32\notepad.exe
3. start.batを実行する

実行結果

※一部抜粋
c:\result.txt

20200725154528235 13592 50.60: trcapi64: 001 +GetCommandLineW()
20200725154528235 13592 50.60: trcapi64: 001 -GetCommandLineW() -> C:\Windows\System32\notepad.exe
20200725154528235 13592 50.60: trcapi64: 001 +CoCreateGuid(7ff6eaac2178)
20200725154528235 13592 50.60: trcapi64: 001   +DeviceIoControl(14c,390008,0,7ff800000000,424a5eed08,30,424a5eece8,0)
20200725154528235 13592 50.60: trcapi64: 001   -DeviceIoControl(,,,,,,,) -> 1
20200725154528235 13592 50.60: trcapi64: 001 -CoCreateGuid() -> 0
20200725154528235 13592 50.60: trcapi64: 001 +CoInitializeEx(0,2)
20200725154528235 13592 50.60: trcapi64: 001   +GetCurrentThreadId()
20200725154528235 13592 50.60: trcapi64: 001   -GetCurrentThreadId() -> 25c8
20200725154528235 13592 50.60: trcapi64: 001   +CreateEventW(0,1,0,<NULL>)
20200725154528235 13592 50.60: trcapi64: 001   -CreateEventW(,,,) -> 15c
20200725154528235 13592 50.60: trcapi64: 001   +CreateEventW(0,0,0,<NULL>)
20200725154528235 13592 50.60: trcapi64: 001   -CreateEventW(,,,) -> 160
20200725154528235 13592 50.60: trcapi64: 001   +CreateEventW(0,0,0,<NULL>)
20200725154528235 13592 50.60: trcapi64: 001   -CreateEventW(,,,) -> 164
20200725154528235 13592 50.60: trcapi64: 001   +GetCurrentThreadId()
20200725154528235 13592 50.60: trcapi64: 001   -GetCurrentThreadId() -> 25c8
20200725154528235 13592 50.60: trcapi64: 001   +GetCurrentThreadId()
20200725154528235 13592 50.60: trcapi64: 001   -GetCurrentThreadId() -> 25c8
20200725154528235 13592 50.60: trcapi64: 001   +RegisterClassW(424a5ef7f0)
20200725154528235 13592 50.60: trcapi64: 001   -RegisterClassW() -> c03c
20200725154528235 13592 50.60: trcapi64: 001   +CreateWindowExW(0,#C03C#,OleMainThreadWndName,7ff888000000,80000000,80000000,4280000000,80000000,fffffffffffffffd,0,7ff8e6f90000,0)
20200725154528236 13592 50.60: trcapi64: 001     +GetCurrentProcessId()