1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

VB.netのプロジェクトにlog4netを使用してログ機能を追加

Last updated at Posted at 2021-02-05

まとめ

log4netを使うためには
①プロジェクトにlog4netを追加(NuGet経由)
②ログが必要のモジュールにAssemblyInfo.vbファイルに追記
③Exeプロジェクトにlog4netのconfigを追加

NuGetでlog4netをプロジェクトに追加

(ネットにたくさんあるので割愛)

AssemblyInfo.vb

基本的にはEXEモジュールにこのファイルに下記内容を追加すればよい。
※クラスライブラリでもログを出力するために、
クラスライブラリプロジェクトのAssemblyInfo.vbに
下記の内容の追加は必要

<Assembly: log4net.Config.XmlConfigurator(ConfigFile:="log4net.config", Watch:=True)>

※ここでlog4netがどの設定ファイル(ここはlog4net.config)を使用するかを指定している。
 指定しない場合、下記のようにApp.configがlog4netの設定ファイルとして使われる。

A.exe(APPメイン処理、B.dllを経由してC.dllを使っている)
B.dll(C.dllを使っている)
C.dll(ログを出力したい処理がある)

この場合、上記内容をAssemblyInfo.vbに追加するのはA.exeとC.dllになる。

log4net.config

log4netを設定するには
A.exeのlog4net.configのみを下記内容を追記すればよい。
①Aのプロジェクトに「新しい項目」を追加して、
 「共通項目」→「全般」→「アプリケーション構成ファイル」を選択し
 「名前」に「log4net.config」を入力して「追加」
②「出力ディレクトリにコピー」を「常にコピーする」を選択
※log4net.configを作成しない場合、A.exeのプロジェクトのApp.configに追加してもよい。
 プロジェクトが作成される際、<プロジェクト名>.exe.configに自動的に追加されるらしい。

  <configSections>
    <section name="log4net"
      type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
  </configSections>

  <appSettings>
    <!-- log4net 内部のデバッグメッセージを出力 -->
    <add key="log4net.Internal.Debug" value="true" />
  </appSettings>

  <log4net>

    <!-- ファイル出力用のアペンダ(日付でローテーション) -->
    <appender name="DailyFileAppender"
      type="log4net.Appender.RollingFileAppender">
      <!-- ログファイルの切替 { サイズ: Size, 日付: Date } -->
      <param name="RollingStyle" value="Date" />
      <!-- ファイル名 -->
      <param name="File" value="log.txt" />
      <!-- ファイル書き込み { 追記: true, 上書き: false } -->
      <param name="AppendToFile" value="true" />
      <!-- 出力文字列のフォーマット -->
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%-5level %date [%thread] %c %line - %message%newline" />
      </layout>
    </appender>

    <!-- デフォルトの出力設定 -->
    <root>
      <level value="ALL" />
      <appender-ref ref="DailyFileAppender" />
    </root>
  </log4net>
1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?