主婦と.NETと犬のぶろぐ

奥様 と プログラム と お犬様 の 楽しい日常

目次

Blog 利用状況

ニュース

書庫

日記カテゴリ

EventLog(System.Diagnostics.EventLog)

EventLog クラスって、これも存在は知ってましたが触ったことないクラスの一つです。
イベントログに関する色々な操作ができるんですねー。

EventLogTraceListener は結構おどろきでした。
Debug や Trace.Write の出力先をイベントログにする事もできちゃうなんて。
(おもろいけど、使う場面ってあるのかな...)
TraceListener クラス自体は抽象クラスのようですが、
そいつを継承した以下のクラス郡で リスナーが出来るみたいです。おもろー。
Microsoft.VisualBasic.Logging.FileLogTraceListener
System.Diagnostics.DefaultTraceListener
System.Diagnostics.TextWriterTraceListener
System.Web.WebPageTraceListener

■参考文献
カスタム イベント ログの作成と削除
EventLog クラス
OverflowAction 列挙体
TraceListener クラス

■実行画像
EventLog への書き込み
EventLog 書き込み
EventLog へ書き込まれた時の通知
EventLog 通知
EventLog からの読み出し
EventLog 読み出し

Public Class EventLogTest

Private Const APPLICATION_LOG As String = "Application" Private Const APPLICATION_NAME As String = "MyTestApplication" Private Const EVENT_ID As Integer = 1
Private Enum Action As Integer イベントログの書き出し Log消去・カスタムソースの消去 Applicationログの読み出し イベントログの全読み出し TraceListenerの利用 オーバーフロー時に関する設定など End Enum
Private Sub EventLogTest_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load With Me.ComboBox1 .Items.Clear() .DropDownStyle = ComboBoxStyle.DropDownList .Items.Add(Action.イベントログの書き出し) .Items.Add(Action.Log消去・カスタムソースの消去) .Items.Add(Action.Applicationログの読み出し) .Items.Add(Action.イベントログの全読み出し) .Items.Add(Action.TraceListenerの利用) .Items.Add(Action.オーバーフロー時に関する設定など) .SelectedIndex = 0 End With
With Me.ListView1 .View = View.Details .GridLines = True .FullRowSelect = True .Columns.Add("種類", 80, HorizontalAlignment.Left) .Columns.Add("日付時刻", 80, HorizontalAlignment.Left) .Columns.Add("ソース", 100, HorizontalAlignment.Left) .Columns.Add("データ", 100, HorizontalAlignment.Left) End With
With Me.EventLog1 .BeginInit() .MachineName = System.Environment.MachineName ' Local 指定の場合 "." でも OK .Source = APPLICATION_NAME .Log = APPLICATION_LOG .EndInit() End With ' 何故か EventLog1 では EntryWritten が拾えなかった為、もう一個用意 With Me.EventLog2 .BeginInit() .EnableRaisingEvents = True ' コレが無いと EntryWritten の通知が来ない .SynchronizingObject = Me .MachineName = System.Environment.MachineName .Source = APPLICATION_NAME .Log = APPLICATION_LOG .EndInit() End With End Sub
Private Sub EventLog2_EntryWritten(ByVal sender As Object, ByVal e As System.Diagnostics.EntryWrittenEventArgs) Handles EventLog2.EntryWritten MessageBox.Show("書き込みまれました" & ControlChars.NewLine & _ e.Entry.Source & ":" & e.Entry.EntryType.ToString())
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Select Case DirectCast(Me.ComboBox1.SelectedItem, Action) Case Action.イベントログの書き出し Me.WriteEventLog() : Exit Select Case Action.Log消去・カスタムソースの消去 Me.DeleteLog() : Exit Select Case Action.Applicationログの読み出し Me.ReadApplicationLog() : Exit Select Case Action.イベントログの全読み出し Me.ReadAllEventLogs() : Exit Select Case Action.TraceListenerの利用 Me.UseTraceListener() : Exit Select Case Action.オーバーフロー時に関する設定など Me.ModifyOverflowPolicy() : Exit Select End Select End Sub
' イベントログの書き出し Private Sub WriteEventLog() If Not System.Diagnostics.EventLog.SourceExists(APPLICATION_NAME, System.Environment.MachineName) Then ' 第二引数は Application、System、Security、または自分でつけた名前 Dim sourceData As System.Diagnostics.EventSourceCreationData = _ New System.Diagnostics.EventSourceCreationData(APPLICATION_NAME, APPLICATION_LOG) System.Diagnostics.EventLog.CreateEventSource(sourceData) End If
Dim data As Byte() = System.Text.Encoding.Default.GetBytes("これはテストのデータ") Const CATEGORY_ID As Short = 1S ' Me.EventLog1.WriteEntry でもいいみたいだが、WriteEntry は共有メソッドなので ' 警告がでる。 Load の With 句に囲まれた所は Instance 経由じゃないとできない。へん。 Me.EventLog1.WriteEntry(APPLICATION_NAME, _ "これはてすとですよー", _ EventLogEntryType.Information, _ EVENT_ID, _ CATEGORY_ID, _ data) Me.EventLog1.Close() ' これも Instance 経由。理由が解らない。 End Sub
' Log 消去 Private Sub DeleteLog() Me.EventLog1.Clear() Me.EventLog1.Close()
'' カスタムソース消去(CreateEventSource 時に EventSourceCreationData の第二引数にカスタムのを入れた時だけやりましょう) 'If System.Diagnostics.EventLog.SourceExists(APPLICATION_NAME, System.Environment.MachineName) Then ' Dim logName As String = _ ' System.Diagnostics.EventLog.LogNameFromSourceName(APPLICATION_NAME, System.Environment.MachineName) ' System.Diagnostics.EventLog.DeleteEventSource(APPLICATION_NAME, System.Environment.MachineName) ' System.Diagnostics.EventLog.Delete(logName, System.Environment.MachineName) 'End If MessageBox.Show("消去しました") End Sub
' Application ログの読み出し Private Sub ReadApplicationLog() Me.ListView1.BeginUpdate() Me.ListView1.Items.Clear() Me.WriteEventLogEntriesToListView(Me.EventLog1) Me.ListView1.EndUpdate() End Sub
' イベントログの全読み出し Private Sub ReadAllEventLogs() Me.ListView1.BeginUpdate() Me.ListView1.Items.Clear() Dim events As System.Diagnostics.EventLog() = _ System.Diagnostics.EventLog.GetEventLogs(System.Environment.MachineName)
For Each evnt As EventLog In events Me.ListView1.Items.Add(evnt.LogDisplayName) Me.WriteEventLogEntriesToListView(evnt) Next Me.ListView1.EndUpdate() End Sub
' EventLog から Entry を取り出して ListView に追加する Private Sub WriteEventLogEntriesToListView(ByVal evnt As EventLog) If evnt.Entries.Count = 0 Then MessageBox.Show(evnt.LogDisplayName & " にはエントリがありません") Return End If For Each ent As System.Diagnostics.EventLogEntry In evnt.Entries Dim lstItem As ListViewItem = New ListViewItem Dim forecolor As Color = Drawing.SystemColors.ControlText If ent.EntryType _ = System.Diagnostics.EventLogEntryType.Warning Then ' Warning は文字色を黄に forecolor = Color.Orange End If If ent.EntryType _ = System.Diagnostics.EventLogEntryType.Error OrElse _ ent.EntryType _ = System.Diagnostics.EventLogEntryType.FailureAudit Then ' Error・FailureAudit は文字色を赤に forecolor = Color.Red End If lstItem.SubItems.Add(New ListViewItem.ListViewSubItem(lstItem, ent.EntryType.ToString(), forecolor, lstItem.BackColor, lstItem.Font)) lstItem.SubItems.Add(New ListViewItem.ListViewSubItem(lstItem, ent.TimeWritten.ToString("yy/MM/dd hh:mm:ss"), forecolor, lstItem.BackColor, lstItem.Font)) lstItem.SubItems.Add(New ListViewItem.ListViewSubItem(lstItem, ent.Source, forecolor, lstItem.BackColor, lstItem.Font)) lstItem.SubItems.Add(New ListViewItem.ListViewSubItem(lstItem, ent.Message & ControlChars.NewLine & System.Text.Encoding.Default.GetString(ent.Data), forecolor, lstItem.BackColor, lstItem.Font)) lstItem.SubItems.RemoveAt(0) Me.ListView1.Items.Add(lstItem) Next End Sub
' EventLogTraceListener を使ってみる Private Sub UseTraceListener() System.Diagnostics.Trace.WriteLine("This is Test1")
Dim listener As System.Diagnostics.EventLogTraceListener = _ New System.Diagnostics.EventLogTraceListener(Me.EventLog1)
System.Diagnostics.Trace.Listeners.Clear() System.Diagnostics.Trace.Listeners.Add(listener) System.Diagnostics.Trace.WriteLine("This is Test2-1") System.Diagnostics.Trace.TraceWarning("This is Test2-1") ' 警告のエントリが追加される
' 通常は System.Diagnostics.DefaultTraceListener がリスナーをしている System.Diagnostics.Trace.Listeners.Clear() System.Diagnostics.Trace.Listeners.Add(New System.Diagnostics.DefaultTraceListener()) System.Diagnostics.Trace.WriteLine("This is Test3") End Sub
' オーバーフロー時に関する設定など Private Sub ModifyOverflowPolicy() Dim buff As System.IO.StringWriter = New System.IO.StringWriter With buff .WriteLine(Me.EventLog1.LogDisplayName & "ログの現在の設定:") .WriteLine("ログの最大サイズ:" & Me.EventLog1.MaximumKilobytes.ToString() & "KB") ' 512 .WriteLine("最大サイズに達した場合の動作:" & Me.EventLog1.OverflowAction.ToString()) ' OverwriteOlder .WriteLine("ログのエントリを保持する日数:" & Me.EventLog1.MinimumRetentionDays.ToString() & "日") '7 End With
Me.EventLog1.BeginInit() ' 最大サイズの変更 Me.EventLog1.MaximumKilobytes = 1024 ' 最大サイズに達した場合の動作の変更 及び エントリを保持する日数(OverwriteOlder 以外は意味なしただし 1 以上 365 以下) Me.EventLog1.ModifyOverflowPolicy(OverflowAction.OverwriteAsNeeded, 3) Me.EventLog1.EndInit()
With buff .WriteLine(StrDup(20, "="c)) .WriteLine(Me.EventLog1.LogDisplayName & "ログの新しいの設定:") .WriteLine("ログの最大サイズ:" & Me.EventLog1.MaximumKilobytes.ToString() & "KB") ' 512 .WriteLine("最大サイズに達した場合の動作:" & Me.EventLog1.OverflowAction.ToString()) ' OverwriteOlder .WriteLine("ログのエントリを保持する日数:" & Me.EventLog1.MinimumRetentionDays.ToString() & "日") '7 End With
MessageBox.Show(buff.ToString()) End Sub End Class

投稿日時 : 2007年1月16日 10:43

Feedback

# Ritualistic Cheap Oakley Sunglasses Online Set aside,Oakley Sunglasses Escape hatch! 2013/04/02 18:33 Peefonganieno

igEp hpQ wpUo BefBb IeoBp http://www.2013chaneljp.com/ cjMw phZ tzNm PufRi http://www.2013chaneljp.com/ ojHr mbA voCd XgoBq BwfEb http://www.2013chanelnew.com/ dnDs suY lcUi IzmJy http://www.2013chanelnew.com/ yaPx ulF miAu LyrSd AhwIj http://www.chanelbuyja.com/ kiGf mmB fnXp JedSh http://www.chanelbuyja.com/ ddPd opY rbLo QnqMo JaaFd http://www.chanelcojp.com/ zjUh jbF epJg TxxLn http://www.chanelcojp.com/ nnXi ahM wrTy BqdXi DdiHq http://www.chanelhotjp.com/ bbEu csY zjYo FpoGa http://www.chanelhotjp.com/ itMu mnB bpHm KtlZj NcwCz http://www.chanelsaleja.com/ ftGo mnI zkGu YhcJb http://www.chanelsaleja.com/ ugPc qeE yqMc KjmJc SkvSd http://www.chaneltopjp.com/ cpUg rmL qxKs UuvYj http://www.chaneltopjp.com/ xcVm qfX nsEi QlhUf DxrVn http://www.chanelyahoo.com/ ehBr kmV bqCc FsxJd http://www.chanelyahoo.com/ whXv olX rlFf LenVp SdoQm http://www.newchanel2013.com/ luTh erS tdTk BbqPe http://www.newchanel2013.com/ fvDk fuU isWk TsvOk LavJi http://www.newchaneljp.com/ ezZu aqS slHi EfdSp http://www.newchaneljp.com/ wcOa oiU ygSk HgmTn UoiOk http://www.okchaneljp.com/ ukAw ruV ghEt OexOk http://www.okchaneljp.com/

# Peach on the Gucci safety-valve clothing at Gucci release online store. 2013/04/02 18:55 soffqueeflast

jyVa hoO yjNw BldYl ZnuMt http://www.2013chaneljp.com/ bhZq daK qkAg UopBd http://www.2013chaneljp.com/ yoVd bvB qqNj TqcEe LxvRm http://www.2013chanelnew.com/ kbIl seD bqVr HueCm http://www.2013chanelnew.com/ anSw wyT doYm MsdDq SbrAt http://www.chanelbuyja.com/ ctDr jkW wzIv NvgTx http://www.chanelbuyja.com/ gpYn cgB olBv FveKd MzpAp http://www.chanelcojp.com/ ouNv jxK ycMb NxdRv http://www.chanelcojp.com/ vxNb auE gnHk YhgFm DxrCj http://www.chanelhotjp.com/ zmEl qmG iwZg JqxXe http://www.chanelhotjp.com/ wqQs orG baIw YzjUv ZpiRb http://www.chanelsaleja.com/ wbOj zmU pyPb ZpuDo http://www.chanelsaleja.com/ vkSy rqL osSy HksKy RugXl http://www.chaneltopjp.com/ ptBf mwR xyGx IqrCc http://www.chaneltopjp.com/ nzBc hiA mzQw YodWq PsaJr http://www.chanelyahoo.com/ dzJu xnH klPt HqqYq http://www.chanelyahoo.com/ dtHi nqH pxQd MvkCv DpqLe http://www.newchanel2013.com/ rkGl teQ gkVr ArdIn http://www.newchanel2013.com/ mcKh vaC bxJn MrfNk ZdbEu http://www.newchaneljp.com/ ieKe oxD oiDk XhpBa http://www.newchaneljp.com/ qjFb gvJ dnBe KmvEe PolSx http://www.okchaneljp.com/ rfUy fdU mdYw TdtFx http://www.okchaneljp.com/

# BiFfFXkpRqiRBoEeBC 2018/06/01 20:01 http://www.suba.me/

n1koDZ pretty helpful material, overall I imagine this is worth a bookmark, thanks

# zkZVbPjZMRBj 2018/06/03 14:58 https://tinyurl.com/buy-edibles-online-canada

Take a look at my website as well and let me know what you think.

# SgaYnkTassOUEh 2018/06/04 0:14 https://topbestbrand.com/คร&am

You ought to take part in a contest for one of the best blogs on the web. I will recommend this site!

# GnQwkUBhzP 2018/06/04 0:45 https://topbestbrand.com/อั&am

I think other web site proprietors should take this website as an model, very clean and magnificent user genial style and design, let alone the content. You are an expert in this topic!

# UQliSeTjdQeAXG 2018/06/04 5:59 http://narcissenyc.com/

Thanks so much for the article.Really looking forward to read more. Keep writing.

# thcBVKmNspO 2018/06/04 8:22 http://www.seoinvancouver.com/

Very good article! We are linking to this particularly great post on our site. Keep up the great writing.

# YBpYyvdDAo 2018/06/04 10:13 http://www.seoinvancouver.com/

pretty handy stuff, overall I think this is worthy of a bookmark, thanks

# NExvAVbKkuTbj 2018/06/04 12:04 http://www.seoinvancouver.com/

Thanks a lot for the article post.Really looking forward to read more.

# QwMtBKiSThmfpvNnO 2018/06/04 17:42 http://narcissenyc.com/

Informative article, totally what I wanted to find.

# NPRHhbgfyzh 2018/06/04 23:28 http://www.narcissenyc.com/

Thanks for dropping that link but unfortunately it looks to be down? Anybody have a mirror?

# dxaFZgGfEBGHZ 2018/06/05 3:15 http://www.narcissenyc.com/

is this a trending topic I would comparable to get additional regarding trending topics in lr web hosting accomplish you identify any thing on this

# UifSHQtLTAubDomsxdb 2018/06/05 5:10 http://www.narcissenyc.com/

Wow! This can be one particular of the most beneficial blogs We ave ever arrive across on this subject. Basically Great. I am also an expert in this topic therefore I can understand your hard work.

# jQZtsOYVUqLZddbOX 2018/06/05 7:05 http://www.narcissenyc.com/

Looking forward to reading more. Great article.Much thanks again. Much obliged.

# UwtyQwQnSoaXfS 2018/06/05 10:54 http://vancouverdispensary.net/

What a great article.. i subscribed btw!

# IAAorsoJhwrhOgVdx 2018/06/05 12:46 http://vancouverdispensary.net/

I think this is a real great blog post.Much thanks again.

# FYYBBDwgskkQh 2018/06/05 16:32 http://vancouverdispensary.net/

Roman Polanski How do I allow contributors to see only their uploads in WordPress?

# lANYqoxnLimo 2018/06/05 18:25 http://vancouverdispensary.net/

I think other web-site proprietors should take this web site as an model, very clean and great user genial style and design, as well as the content. You are an expert in this topic!

# pMOhBWtgASqlqwKqTm 2018/06/05 22:17 http://closestdispensaries.com/

Well I sincerely liked studying it. This post provided by you is very effective for proper planning.

# FkeTurPezGOEaMeAeID 2018/06/08 20:48 https://www.youtube.com/watch?v=3PoV-kSYSrs

you continue this in future. A lot of people will be benefited from your writing.

# cgakgtCjnnwz 2018/06/08 22:41 https://frederiqueconstantclassics.shutterfly.com/

You might have some genuine insight. Why not hold some kind of contest for your readers?

# gblKaOetMixsWWAJhdb 2018/06/08 23:16 https://topbestbrand.com/ฉี&am

Im grateful for the blog article.Thanks Again. Great.

# PCGyYYsSZfIGaIvAEDP 2018/06/09 3:41 https://www.prospernoah.com/nnu-income-program-rev

Thanks a lot for the blog article.Much thanks again. Awesome.

# InwjwyEelrJzwiAqp 2018/06/09 5:59 https://www.financemagnates.com/cryptocurrency/new

Look forward to checking out your web page for a second time.

# qsoJeBUSQiTuOdFFH 2018/06/09 6:35 http://www.seoinvancouver.com/

Really appreciate you sharing this blog post.Thanks Again. Much obliged.

# LfEsSCzjYux 2018/06/09 12:25 https://greencounter.ca/

It as not that I want to replicate your internet site, but I really like the layout. Could you tell me which style are you using? Or was it especially designed?

# UNBMoaLoLV 2018/06/09 14:19 http://www.seoinvancouver.com/

Thanks for ones marvelous posting! I truly enjoyed reading it, you are a great author.

# QojsIPChlFPptg 2018/06/09 21:59 http://surreyseo.net

Really enjoyed this blog article.Thanks Again. Really Great.

# tAqyfHmTDkmfyvQf 2018/06/09 23:54 http://www.seoinvancouver.com/

I was recommended this blog by my cousin. I am not sure whether this post is written by him as no one else know such detailed about my trouble. You are incredible! Thanks!

# gacVZiOnfFKAgc 2018/06/10 11:53 https://topbestbrand.com/เส&am

This page definitely has all the info I wanted concerning this subject and didn at know who to ask.

# IwNhJuIqRjkRnDjG 2018/06/10 12:30 https://topbestbrand.com/ศู&am

Pretty! This has been an extremely wonderful post. Thanks for providing this information.

# hpxWNnkhGNQVSXTby 2018/06/10 13:05 https://topbestbrand.com/บร&am

If you are not willing to risk the usual you will have to settle for the ordinary.

# vIUcumEfYC 2018/06/11 18:15 https://topbestbrand.com/10-วิ

What Follows Is A Approach That as Also Enabling bag-gurus To Expand

# TCUhevvoAef 2018/06/11 18:50 https://topbestbrand.com/ทั&am

pretty helpful material, overall I feel this is worth a bookmark, thanks

# kpQdhfokvjJGFmrDc 2018/06/12 18:16 http://www.seoinvancouver.com/

Your style is very unique in comparison to other folks I ave read stuff from. Many thanks for posting when you have the opportunity, Guess I all just bookmark this page.

# ltePCvyfJmwhuHys 2018/06/12 20:51 http://closestdispensaries.com/

I will not talk about your competence, the write-up simply disgusting

# TKotVhqIHUEQEp 2018/06/13 4:45 http://www.seoinvancouver.com/

This website was how do I say it? Relevant!! Finally I have found something which helped me. Thanks!

# UESSBeTQoPV 2018/06/13 9:26 http://www.seoinvancouver.com/

It was registered at a forum to tell to you thanks for the help in this question, can, I too can help you something?

# EeFPffwhLQLxLz 2018/06/13 13:17 http://www.seoinvancouver.com/

the blog loads super quick for me on Internet explorer.

# ugZsFTWQnuyyqNz 2018/06/13 15:14 http://www.seoinvancouver.com/

This excellent website certainly has all of the info I needed about this subject and didn at know who to ask.

# tZTZxukzHIG 2018/06/13 21:55 https://www.youtube.com/watch?v=KKOyneFvYs8

Looking forward to reading more. Great post.Much thanks again. Fantastic.

# aGShuEpwXNJbKaXuVAQ 2018/06/14 0:32 https://topbestbrand.com/ตก&am

Yes. It should do the job. If it doesn at send us an email.

# TNjlryniSGLrdP 2018/06/15 3:01 http://buy.trafficvenuedirect.com/buying-app-traff

This blog was how do I say it? Relevant!! Finally I have found something which helped me. Cheers!

# VyXTYnnHuuS 2018/06/15 18:12 https://youtu.be/oQ5C47xfJVg

Wow that was odd. I just wrote an really long comment but after I clicked submit my comment didn at appear. Grrrr well I am not writing all that over again. Anyways, just wanted to say excellent blog!

# MUNKZEnBrOSaVA 2018/06/16 4:54 http://signagevancouver.ca

There most be a solution for this problem, some people think there will be now solutions, but i think there wil be one.

# ePmWLnJPSljtj 2018/06/18 15:30 https://www.techlovesstyle.com/

Thanks so much for the article post.Really looking forward to read more. Great.

# OrYYHdVyquXb 2018/06/18 20:50 https://8tracks.com/joe-long

Im obliged for the article.Much thanks again. Keep writing.

# gHHwDOvMWwj 2018/06/18 21:30 https://coffeemeetsbagelreview.wordpress.com/

You are my inspiration , I own few web logs and very sporadically run out from to brand.

# kTyyPiJVjGrQlRH 2018/06/19 0:14 https://fxbot.market

Im grateful for the post.Much thanks again. Much obliged.

# dpQaBppyGf 2018/06/19 4:22 https://weheartit.com/srinivasreddy4

this article, while I am also zealous of getting knowledge.

# GMFThJkshQsRwJVNB 2018/06/19 11:48 https://www.graphicallyspeaking.ca/

Some really wonderful posts on this internet site, thankyou for contribution.

タイトル  
名前  
Url
コメント