LoginSignup
3
3

More than 5 years have passed since last update.

IISログをC#で操作する。

Last updated at Posted at 2015-10-16

IISログをC#から取得する必要に迫られたので、ノウハウを残します。
一番古いログが取れてくるため、日付け順に並び替える処理がどうしても遅くなるので、Count取得して要素を取得した方が良いです。

IISLog.cs
                //取得するイベントログ名
                string logName = "Application";
                //コンピュータ名
                string machineName = "XXXXXX";

                //指定したイベントログが存在しているか調べる
                if (System.Diagnostics.EventLog.Exists(logName, machineName))
                {
                    //EventLogオブジェクトを作成する
                    System.Diagnostics.EventLog log =
                        new System.Diagnostics.EventLog(logName, machineName);

                    // ログエントリをすべて取得する
                    var count = 0;
                    foreach (EventLogEntry entry in log.Entries.Cast<EventLogEntry>().OrderByDescending(x => x.TimeGenerated))
                    {
                    // 対象のログを判断する方法を記述
                        if (entry.ReplacementStrings.Any(x => x == "hoge"))
                        {
                             //何か処理
                        }
                    }
                    //閉じる
                    log.Close();
                }
3
3
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
3
3