ODBC ログ <odbcLogging>

概要

<odbcLogging> 要素は、インターネット インフォメーション サービス (IIS) 7 の Open Database Connectivity (ODBC) ログを構成します。 ODBC ログでは、テキスト ファイルではなく、ユーザー定義データベースにすべての HTTP アクティビティが記録されます。

ODBC ログは IIS 7 のカスタム ログ モジュールとして実装されているため、IIS 7 での ODBC ログの有効化と構成には、次の 2 つのアクションで構成されます。

  • <odbcLogging> 要素内の ODBC ログ属性を設定します。 具体的には、次の属性を構成する必要があります。

    • dataSource 属性は、tableName 属性で指定されたテーブルが配置されているデータベースのシステム データ ソース名 (DSN) を指定する必要があります。
    • password 属性は、IIS 7 がデータベースにログ情報を書き込むときに使用するデータベース パスワードを指定する必要があります。
    • tableName 属性は、dataSource 属性で指定されたデータベース内のデータベース テーブルの名前を指定する必要があります。
    • userName 属性は、IIS 7 がデータベースにログ情報を書き込むときに使用するデータベース ユーザー名を指定する必要があります。
  • <logFile> 要素で適切なカスタム ログ属性を設定します。 具体的には、次の属性を構成する必要があります。

    • logFormat 属性は "Custom" に設定する必要があります。
    • customLogPluginClsid 属性を "{FF16065B-DE82-11CF-BC0A-00AA006111E0}" に設定する必要があります。

既定では、IIS 7 は W3C 拡張ログ ファイル形式を使用するテキスト ファイルにすべてのアクティビティを記録します。さらに、既定では、NCSA やネイティブ IIS 形式などの他のテキスト ファイル形式を使用するように IIS を構成できます。 これらのテキスト ベースの形式を使用する主な欠点は、アクティビティの解析が困難であり、一般に、ログから有用な情報を取得するために Microsoft の LogParser のようなユーティリティが必要になることです。 さらに、テキスト ファイルでのデータ解析のパフォーマンスは、通常、データベースを使用する場合よりもはるかに低速です。 ただし、テキスト ベースのログ記録はカーネル モードで実行されるため、要求のログ記録のパフォーマンスが向上します。

ODBC ログを使用すると、サーバーのアクティビティがデータベースに格納されるため、データ取得が大幅に向上します。 これにより、サーバーのアクティビティをデータ マイニングするときに、さまざまなデータベース クライアントを使用する可能性が広がります。 ODBC ログを使用する場合の主な欠点は、ログ記録のパフォーマンスです。これは、ODBC ログ記録が有効になっていると、IIS がカーネル モード キャッシュを無効にするためです。 このため、ODBC ログを実装すると、サーバーの全体的なパフォーマンスが低下する可能性があります。

Note

ODBC ログ用のテーブルを作成するには、IIS 7 で提供されている "%windir%\System32\inetsrv\logtemp.sql" ファイルを使用できます。 ODBC ログの詳細については、Microsoft サポート技術情報の記事 245243 を参照してください。

互換性

バージョン メモ
IIS 10.0 <odbcLogging> 要素は、IIS 10.0 では変更されませんでした。
IIS 8.5 <odbcLogging> 要素は、IIS 8.5 では変更されませんでした。
IIS 8.0 <odbcLogging> 要素は IIS 8.0 では変更されませんでした。
IIS 7.5 <odbcLogging> 要素は、IIS 7.5 では変更されませんでした。
IIS 7.0 <odbcLogging> 要素が IIS 7.0 で導入されました。
IIS 6.0 <odbcLogging> 要素の属性は、次の IIS 6.0 メタベース プロパティを置き換えます。
  • LogOdbcDataSource
  • LogOdbcPassword
  • LogOdbcTableName
  • LogOdbcUserName

段取り

<odbcLogging> 要素は、IIS 7 以降の既定のインストールでは使用できません。 インストールするには、次の手順を使用します。

Windows Server 2012 または Windows Server 2012 R2

  1. タスク バーで [サーバー マネージャー]をクリックします。
  2. [サーバー マネージャー] で、[管理] メニューをクリックし、[役割と機能の追加] をクリックします。
  3. 役割と機能の追加ウィザードで、[次へ] をクリックします。 [インストールの種類] を選択し、[次へ] をクリックします。 対象サーバーを選択し、[次へ] をクリックします。
  4. [サーバーの役割] ページで、[Web サーバー (IIS)][Web サーバー] の順に展開し、[正常性と診断] を展開して、[ODBC ログ] を選択します。 次へ をクリックします。
    Screenshot shows the Health and Diagnostics features for Windows Server 2012 or Windows Server 2012 R2 with O D B C Logging selected.
  5. [機能の選択] ページで、[次へ] をクリックします。
  6. [インストール オプションの確認] ページで、[インストール] をクリックします。
  7. [結果] ページで、 [閉じる]をクリックします。

Windows 8 または Windows 8.1

  1. [スタート] 画面で、ポインターを左下隅まで移動し、[スタート] ボタンを右クリックし、[コントロール パネル] をクリックします。
  2. [コントロール パネル][プログラムと機能] をクリックし、[Windows の機能の有効化または無効化] をクリックします。
  3. [インターネット インフォメーション サービス] を展開し、[World Wide Web サービス] を展開し、[正常性と診断] を展開して、[ODBC ログ] を選択します。
    Screenshot shows the Health and Diagnostics features for Windows 8 or Windows 8.1 with O D B C Logging selected.
  4. OK をクリックします。
  5. 閉じるをクリックします。

Windows Server 2008 または Windows Server 2008 R2

  1. タスク バーで [スタート] をクリックし、[管理ツール] をポイントして、[サーバー マネージャー] をクリックします。
  2. [Server Manager] (サーバー マネージャー) 階層ウィンドウで [ロール] を展開し、[Web Server (IIS)] (Web サーバー (IIS)) をクリックします。
  3. [Web Server (IIS)] (Web サーバー (IIS)) ウィンドウで、[Role Services] (役割サービス) セクションまでスクロールし、[Add Role Services] (役割サービスの追加) をクリックします。
  4. 役割サービスの追加ウィザード[役割サービスの選択] ページで、[ODBC ログ] を選択し、[次へ] をクリックします。
    Screenshot shows the Health and Diagnostics features for Windows Server 2008 or Windows Server 2008 R2 with O D B C Logging selected.
  5. [インストール オプションの確認] ページで、[インストール] をクリックします。
  6. [結果] ページで、 [閉じる]をクリックします。

Windows Vista または Windows 7

  1. タスク バーで、[スタート][コントロール パネル] の順にクリックします。
  2. [コントロール パネル][プログラムと機能] をクリックし、[Turn Windows Features on or off] (Windows の機能の有効化または無効化) をクリックします。
  3. [インターネット インフォメーション サービス] を展開し、[ODBC ログ] を選択して、[OK] をクリックします。
    Screenshot shows the Health and Diagnostics features for Windows Vista or Windows 7 with O D B C Logging selected.

操作方法

IIS 7 の ODBC ログを構成するためのユーザー インターフェイスはありません。 プログラムで ODBC ログを構成する方法の例については、このドキュメントの「コード サンプル」セクションを参照してください。

構成

属性

属性 説明
dataSource 省略可能な文字列属性。 ログの書き込み先データベースのシステム データ ソース名 (DSN) を指定します。

既定値は InternetDb です。
password 省略可能な文字列属性。 イベント ログの記録中にデータベースに情報を書き込むときに使用する ODBC データベース パスワードを指定します。 既定では、この値は暗号化されます。

既定値は [enc:AesProvider::enc] です。
tableName 省略可能な文字列属性。 イベント ログの記録中に Windows が情報を書き込む ODBC データベース テーブルの名前を指定します。

既定値は InternetLog です。
userName 省略可能な文字列属性。 イベント ログの記録中にデータベースに情報を書き込む際に使用する ODBC データベース ユーザー名を指定します。

既定値は InternetAdmin です。

子要素

なし。

構成サンプル

この既定の Web サイトの最初の構成サンプルでは、ODBC ログ接続のシステム DSN、テーブル名、ユーザー名、パスワードを指定する <odbcLogging> 要素の属性を構成します。

<location path="Default Web Site">
   <system.webServer>
      <odbcLogging dataSource="InternetDb"
         tableName="InternetLog"
         userName="InternetAdmin"
         password="[enc:AesProvider:57686f6120447564652c2049495320526f636b73:enc]" />
   </system.webServer>
</location>

この 2 番目の構成サンプルでは、既定の Web サイトに対して ODBC ログを有効にする <logFile> 要素の正しいカスタム ログ属性を構成します。

<site name="Default Web Site" id="1" serverAutoStart="true">
   <application path="/" applicationPool="DefaultAppPool">
      <virtualDirectory path="/" physicalPath="%SystemDrive%\inetpub\wwwroot" />
   </application>
   <bindings>
      <binding protocol="http" bindingInformation="*:80:" />
   </bindings>
   <logFile logFormat="Custom"
      customLogPluginClsid="{FF16065B-DE82-11CF-BC0A-00AA006111E0}" />
</site>

サンプル コード

次のコード サンプルでは、既定の Web サイトに対して 2 つのオプション セットを構成します。

  • 各サンプルの前半では、ODBC ログ接続のシステム DSN、テーブル名、ユーザー名、パスワードを指定する <odbcLogging> 要素の属性を構成します。
  • 各サンプルの後半では、ODBC ログを有効にする <logFile> 要素のカスタム ログ属性を指定します。

AppCmd.exe

appcmd.exe set config "Default Web Site" -section:system.webServer/odbcLogging /dataSource:"InternetDb" /commit:apphost
appcmd.exe set config "Default Web Site" -section:system.webServer/odbcLogging /tableName:"InternetLog" /commit:apphost
appcmd.exe set config "Default Web Site" -section:system.webServer/odbcLogging /userName:"InternetAdmin" /commit:apphost
appcmd.exe set config "Default Web Site" -section:system.webServer/odbcLogging /password:"P@ssw0rd" /commit:apphost

appcmd.exe set sites "Default Web Site" -logFile.logFormat:"Custom" /commit:apphost
appcmd.exe set sites "Default Web Site" -logFile.customLogPluginClsid:"{FF16065B-DE82-11CF-BC0A-00AA006111E0}" /commit:apphost

Note

AppCmd.exe を使用してこれらの設定を構成する場合は、commit パラメーターを必ず apphost に設定する必要があります。 これにより、ApplicationHost.config ファイルの適切な場所セクションに構成設定がコミットされます。

C#

using System;
using System.Text;
using Microsoft.Web.Administration;

internal static class Sample
{
   private static void Main()
   {
      using (ServerManager serverManager = new ServerManager())
      {
         Configuration config = serverManager.GetApplicationHostConfiguration();

         ConfigurationSection odbcLoggingSection = config.GetSection("system.webServer/odbcLogging", "Default Web Site");
         odbcLoggingSection["dataSource"] = @"InternetDb";
         odbcLoggingSection["tableName"] = @"InternetLog";
         odbcLoggingSection["userName"] = @"InternetAdmin";
         odbcLoggingSection["password"] = @"P@ssw0rd";

         ConfigurationSection sitesSection = config.GetSection("system.applicationHost/sites");
         ConfigurationElementCollection sitesCollection = sitesSection.GetCollection();
         ConfigurationElement siteElement = FindElement(sitesCollection, "site", "name", @"Default Web Site");
         if (siteElement == null) throw new InvalidOperationException("Element not found!");

         ConfigurationElement logFileElement = siteElement.GetChildElement("logFile");
         logFileElement["customLogPluginClsid"] = @"{FF16065B-DE82-11CF-BC0A-00AA006111E0}";
         logFileElement["logFormat"] = @"Custom";

         serverManager.CommitChanges();
      }
   }

   private static ConfigurationElement FindElement(ConfigurationElementCollection collection, string elementTagName, params string[] keyValues)
   {
      foreach (ConfigurationElement element in collection)
      {
         if (String.Equals(element.ElementTagName, elementTagName, StringComparison.OrdinalIgnoreCase))
         {
            bool matches = true;
            for (int i = 0; i < keyValues.Length; i += 2)
            {
               object o = element.GetAttributeValue(keyValues[i]);
               string value = null;
               if (o != null)
               {
                  value = o.ToString();
               }
               if (!String.Equals(value, keyValues[i + 1], StringComparison.OrdinalIgnoreCase))
               {
                  matches = false;
                  break;
               }
            }
            if (matches)
            {
               return element;
            }
         }
      }
      return null;
   }
}

VB.NET

Imports System
Imports System.Text
Imports Microsoft.Web.Administration

Module Sample
   Sub Main()
      Dim serverManager As ServerManager = New ServerManager
      Dim config As Configuration = serverManager.GetApplicationHostConfiguration

      Dim odbcLoggingSection As ConfigurationSection = config.GetSection("system.webServer/odbcLogging", "Default Web Site")
      odbcLoggingSection("dataSource") = "InternetDb"
      odbcLoggingSection("tableName") = "InternetLog"
      odbcLoggingSection("userName") = "InternetAdmin"
      odbcLoggingSection("password") = "P@ssw0rd"

      Dim sitesSection As ConfigurationSection = config.GetSection("system.applicationHost/sites")
      Dim sitesCollection As ConfigurationElementCollection = sitesSection.GetCollection
      Dim siteElement As ConfigurationElement = FindElement(sitesCollection, "site", "name", "Default Web Site")
      If (siteElement Is Nothing) Then
         Throw New InvalidOperationException("Element not found!")
      End If

      Dim logFileElement As ConfigurationElement = siteElement.GetChildElement("logFile")
      logFileElement("customLogPluginClsid") = "{FF16065B-DE82-11CF-BC0A-00AA006111E0}"
      logFileElement("logFormat") = "Custom"

      serverManager.CommitChanges()
   End Sub

   Private Function FindElement(ByVal collection As ConfigurationElementCollection, ByVal elementTagName As String, ByVal ParamArray keyValues() As String) As ConfigurationElement
      For Each element As ConfigurationElement In collection
         If String.Equals(element.ElementTagName, elementTagName, StringComparison.OrdinalIgnoreCase) Then
            Dim matches As Boolean = True
            Dim i As Integer
            For i = 0 To keyValues.Length - 1 Step 2
               Dim o As Object = element.GetAttributeValue(keyValues(i))
               Dim value As String = Nothing
               If (Not (o) Is Nothing) Then
                  value = o.ToString
               End If
               If Not String.Equals(value, keyValues((i + 1)), StringComparison.OrdinalIgnoreCase) Then
                  matches = False
                  Exit For
               End If
            Next
            If matches Then
               Return element
            End If
         End If
      Next
      Return Nothing
   End Function


End Module

JavaScript

var adminManager = new ActiveXObject('Microsoft.ApplicationHost.WritableAdminManager');
adminManager.CommitPath = "MACHINE/WEBROOT/APPHOST";

var odbcLoggingSection = adminManager.GetAdminSection("system.webServer/odbcLogging", "MACHINE/WEBROOT/APPHOST/Default Web Site");
odbcLoggingSection.Properties.Item("dataSource").Value = "InternetDb";
odbcLoggingSection.Properties.Item("tableName").Value = "InternetLog";
odbcLoggingSection.Properties.Item("userName").Value = "InternetAdmin";
odbcLoggingSection.Properties.Item("password").Value = "P@ssw0rd";

var sitesSection = adminManager.GetAdminSection("system.applicationHost/sites", "MACHINE/WEBROOT/APPHOST");
var sitesCollection = sitesSection.Collection;
var siteElementPos = FindElement(sitesCollection, "site", ["name", "Default Web Site"]);
if (siteElementPos == -1) throw "Element not found!";
var siteElement = sitesCollection.Item(siteElementPos);

var logFileElement = siteElement.ChildElements.Item("logFile");
logFileElement.Properties.Item("customLogPluginClsid").Value = "{FF16065B-DE82-11CF-BC0A-00AA006111E0}";
logFileElement.Properties.Item("logFormat").Value = "Custom";

adminManager.CommitChanges();

function FindElement(collection, elementTagName, valuesToMatch) {
   for (var i = 0; i < collection.Count; i++) {
      var element = collection.Item(i);
      if (element.Name == elementTagName) {
         var matches = true;
         for (var iVal = 0; iVal < valuesToMatch.length; iVal += 2) {
            var property = element.GetPropertyByName(valuesToMatch[iVal]);
            var value = property.Value;
            if (value != null) {
               value = value.toString();
            }
            if (value != valuesToMatch[iVal + 1]) {
               matches = false;
               break;
            }
         }
         if (matches) {
            return i;
         }
      }
   }
   return -1;
}

VBScript

Set adminManager = WScript.CreateObject("Microsoft.ApplicationHost.WritableAdminManager")
adminManager.CommitPath = "MACHINE/WEBROOT/APPHOST"

Set odbcLoggingSection = adminManager.GetAdminSection("system.webServer/odbcLogging", "MACHINE/WEBROOT/APPHOST/Default Web Site")
odbcLoggingSection.Properties.Item("dataSource").Value = "InternetDb"
odbcLoggingSection.Properties.Item("tableName").Value = "InternetLog"
odbcLoggingSection.Properties.Item("userName").Value = "InternetAdmin"
odbcLoggingSection.Properties.Item("password").Value = "P@ssw0rd"

Set sitesSection = adminManager.GetAdminSection("system.applicationHost/sites", "MACHINE/WEBROOT/APPHOST")
Set sitesCollection = sitesSection.Collection
siteElementPos = FindElement(sitesCollection, "site", Array("name", "Default Web Site"))
If (siteElementPos = -1) Then
   WScript.Echo "Element not found!"
   WScript.Quit
End If
Set siteElement = sitesCollection.Item(siteElementPos)

Set logFileElement = siteElement.ChildElements.Item("logFile")
logFileElement.Properties.Item("customLogPluginClsid").Value = "{FF16065B-DE82-11CF-BC0A-00AA006111E0}"
logFileElement.Properties.Item("logFormat").Value = "Custom"

adminManager.CommitChanges()

Function FindElement(collection, elementTagName, valuesToMatch)
   For i = 0 To CInt(collection.Count) - 1
      Set element = collection.Item(i)
      If element.Name = elementTagName Then
         matches = True
         For iVal = 0 To UBound(valuesToMatch) Step 2
            Set property = element.GetPropertyByName(valuesToMatch(iVal))
            value = property.Value
            If Not IsNull(value) Then
               value = CStr(value)
            End If
            If Not value = CStr(valuesToMatch(iVal + 1)) Then
               matches = False
               Exit For
            End If
         Next
         If matches Then
            Exit For
         End If
      End If
   Next
   If matches Then
      FindElement = i
   Else
      FindElement = -1
   End If
End Function