投稿者「mynote」のアーカイブ

C#でソースコードの行番号やファイル名を取得する方法

C++言語では__LINE____FILE__のマクロでソースコードの行番号やファイル名を取得することができます。C#でもこれと同じようなことをしたいと思って方法を調べてみました。

StackFrameを使う方法

using System;
using System.Diagnostics;

public class Example1
{
  public static void Main()
  {
    StackFrame stackFrame = new StackFrame(true);

    int fileLineNumber = stackFrame.GetFileLineNumber();
    string fileName = stackFrame.GetFileName();

    Console.WriteLine($"FileLineNumber: {fileLineNumber}");
    Console.WriteLine($"FileName: {fileName}");
  }
}

実行結果

FileLineNumber: 8
FileName: C:\work\example1.cs

CallerLineNumber/CallerFilePathを使う方法

using System;
using System.Runtime.CompilerServices;

public class Example2
{
  public static void Main()
  {
    test();
  }

  static void test([CallerLineNumber] int lineNumber = 0, [CallerFilePath] string filePath = null)
  {
    Console.WriteLine($"LineNumber: {lineNumber}");
    Console.WriteLine($"FilePath: {filePath}");
  }
}

実行結果

LineNumber: 8
FilePath: C:\work\example2.cs

ファイル名のみを取得したい場合

どちらの方法を使っても取得できるのはファイル名ではなく、ファイルパスになっています。ファイル名のみを取得したい場合はGetFileNameを使うと便利です。

string fileName = System.IO.Path.GetFileName(filePath);

リンク

StackFrame Class (System.Diagnostics) | Microsoft Docs
https://docs.microsoft.com/en-us/dotnet/api/system.diagnostics.stackframe?view=net-6.0

System.Runtime.CompilerServices Namespace | Microsoft Docs
https://docs.microsoft.com/en-us/dotnet/api/system.runtime.compilerservices?view=net-6.0

GitHubをプロキシ環境下で使う方法

.gitconfigで設定する方法

以下のコマンドを実行します。

$ git config --global http.https://github.com.proxy http://123.456.789.012:1234

もしくは、$HOME/.gitconfigに以下の内容を追加してください。

[http "https://github.com"]
proxy = 123.456.789.012:1234

※プロキシサーバーへのアクセスにユーザー名やパスワードが必要な場合はhttp://username:[email protected]:1234のような形で設定できます。

.ssh/configで設定する方法

$HOME/.ssh/configに以下の内容を追加します。

Host github.com
ProxyCommand connect -H 123.456.789.012:1234 %h %p

Raspberry Piにプロキシサーバーをインストールする方法

Raspberry Piにプロキシサーバー「Squid」をインストールするメモ。

インストール

$ sudo apt install squid

設定

設定ファイルは/etc/squid/squid.confになります。
お好みのエディタで編集してください。

$ sudo vi /etc/squid/squid.conf

Squidの起動

$ sudo service squid start

起動後、設定ファイルをリロードしたい場合は次のようにします。

$ sudo service squid reload

Squidの設定メモ

使用ポートを3128番に設定して、すべてのアクセスを許可

http_port 3128

acl all src 0.0.0.0/0
http_access allow all

特定のIPアドレスからのアクセスのみ許可

http_port 3128

acl host src 192.168.0.12/32
http_access allow host

acl all src 0.0.0.0/0
http_access deny all

IPアドレスを範囲で許可したい場合は192.168.0.0/24192.168.0.1-192.168.0.255
のような形式で設定することもできます。

特定のドメインのみアクセスを許可

http_port 3128

acl allowlist dstdomain "/etc/squid/allowlist"
http_access allow allowlist

acl all src 0.0.0.0/0
http_access deny all

allowlist

www.example.com
.example.net

のように許可したいドメインを記述します。

.から始まるドメインはサブドメインすべてが許可される設定になります。

特定のIPアドレスから特定のドメインのみアクセスを許可

http_port 3128

acl allowlist dstdomain "/etc/squid/allowlist"
http_access deny !allowlist

acl host src 192.168.0.12/32
http_access allow host

acl all src 0.0.0.0/0
http_access deny all

その他の設定

以下は必要な場合のみ/etc/squid/squid.confに追加してください。

プロキシサーバーを経由しているという情報を隠したい場合

forwarded_for off
request_header_access X-Forwarded-For deny all
request_header_access Via deny all
visible_hostname unknown

リファラー情報を送信したくない場合

request_header_access Referer deny all

リンク

FrontPage – Squid Web Proxy Wiki
https://wiki.squid-cache.org/