IT業界のすみっこ暮らし

ふと気がついたときの記録



log4netのバージョン違い対策(ASP.NET)

log4netのバージョン違い対策(ASP.NET)

A:新しいプロジェクト。当然log4netも最新バージョン。でもBから一部のロジックは参考しないといけない。
# log4netバージョン:1.2.15.0

B:いわゆる共通ロジックを含む古いプロジェクト。log4netのバージョンも古い。
# log4netバージョン:1.2.10.0

…そしてAが単独で動くときは問題なかったけど、Bのロジックを呼び(Add Referencesでdll参照)、そこでログを吐き出すときにエラーが発生した。

エラーメッセージ

'log4net, Version=1.2.10.0, Culture=neutral, PublicKeyToken=1b44e1d426115821'、またはその依存関係の 1 つが読み込めませんでした。見つかったアセンブリのマニフェスト定義はアセンブリ参照に一致しません。 (HRESULT からの例外:0x80131040)

対策

1、Webアプリの場合

1、Webアプリが認識可能な場所に「log4net.1.2.10」のdllを格納する。

2、Web.configにアプリが使用する最新のlog4netと「log4net.1.2.10」の宣言を追加する。
「log4net.1.2.10」の宣言の際に1で格納した「log4net.1.2.10」のdllがあるパスを正しく記入する。

AのWeb.config

<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
  <dependentAssembly>
    <assemblyIdentity name="log4net" culture="neutral" publicKeyToken="669e0ddf0bb1aa2a" />
    <!-- <codeBase version="1.2.15.0" href="log4net.dll" /> 自分が使うlog4netのバージョンは記入しなくても問題ない -->
    <bindingRedirect oldVersion="0.0.0.0-1.2.15.0" newVersion="1.2.15.0" />
  </dependentAssembly>
  <!-- B用 -->
  <dependentAssembly>
    <assemblyIdentity name="log4net" publicKeyToken="1b44e1d426115821" />
    <!-- プロジェクトフォルダからの相対パス。
   この場合、log4net.dllを「C:\project\A\bin\log4net.1.2.10」の中に格納している。 -->
    <codeBase version="1.2.10.0" href="bin\log4net.1.2.10\log4net.dll" />
  </dependentAssembly>
</assemblyBinding>
</runtime>

実際のフォルダだとこんな感じ。
C:\project\A\bin\に当てはまる場所になります。

f:id:papamau:20161222095540p:plain


コンテンツ発行時の注意事項

発行設定(Package/Publish Web)を「All files in this project folder」にしないと、上記で設定した古いlog4net対応が反映されない。

f:id:papamau:20161222095800p:plain

2、Consoleアプリの場合

1、Consoleアプリが認識可能な場所に「log4net.1.2.10」のdllを格納する。(configurationの種類(ex)debug/release)分、格納をする必要がある)

2、App.configにアプリが使用する最新のlog4netと「log4net.1.2.10」の宣言を追加する。
「log4net.1.2.10」の宣言の際に1で格納した「log4net.1.2.10」のdllがあるパスを正しく記入する。

AのApp.config

<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
  <dependentAssembly>
    <assemblyIdentity name="log4net" culture="neutral" publicKeyToken="669e0ddf0bb1aa2a" />
    <!-- <codeBase version="1.2.15.0" href="log4net.dll" /> 自分が使うlog4netのバージョンは記入しなくても問題ない -->
    <bindingRedirect oldVersion="0.0.0.0-1.2.15.0" newVersion="1.2.15.0" />
  </dependentAssembly>
  <!-- B用 -->
  <dependentAssembly>
    <assemblyIdentity name="log4net" publicKeyToken="1b44e1d426115821" />
    <!-- バッチ実行ファイル(ex)C:\project\A\bin\Release\A.exe)からの相対パス。
   この場合、log4net.dllを「C:\project\A\bin\Release\log4net.1.2.10」の中に格納している。    
   -->
    <codeBase version="1.2.10.0" href="bin\log4net.1.2.10\log4net.dll" />
  </dependentAssembly>
</assemblyBinding>
</runtime>




プライバシーポリシー