Attention: We are retiring the ASP.NET Community Blogs. Learn more >

Trying to write to a full event log

This got us this week.  Our app was trying to write exception details to the event log (Application) and the user's log was full.

To 'handle' this, I now handle the 'log is full' exception and ask the user if I can clear the log for them.  Note, to clear you must be an admin.  The below code picks up from the "Log is full" exception:

 

Catch win32Ex As System.ComponentModel.Win32Exception

Dim dr As DialogResult = MessageBox.Show("AM.NET needs to log an exception to the " & logName & " Log, though the log is full." & vbCr & vbCr & _

"AM.NET can clear the log for you or you can clear the log manually. Would you like AM.NET to clear the log for you (note, you must be an administrator to clear the log)?", _

logName & " Log is Full", _

MessageBoxButtons.YesNo, _

MessageBoxIcon.Question)

If dr = DialogResult.Yes Then

Dim myDomain As AppDomain = Thread.GetDomain()

myDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal)

Dim myPrincipal As WindowsPrincipal = CType(Thread.CurrentPrincipal, WindowsPrincipal)

If myPrincipal.IsInRole(WindowsBuiltInRole.Administrator) Then

Dim log As New EventLog

log.Log = logName

log.Source = applicationName

Try

log.Clear()

Me.WriteToLog(entry, type)

Catch ex As Exception

MessageBox.Show(ex.ToString())

Exit Sub

End Try

Else

MessageBox.Show(Environment.UserName & " is not an Administrator. Please log on as an Administrator and manually clear the " & logName & " Log.", _

"Not an Administrator", _

MessageBoxButtons.OK, _

MessageBoxIcon.Information)

End If

5 Comments

  • you can also just set the log to automatically rotate.

    Yes, when it gets full, it will overwrite entries at the begining -

    but that is not always a bad thing, specially if you set the log size to be big enough...

  • Correct, though I cannot 'touch' the user's machines or settings. The user in question had "Do not overwrite events" checked for some reason. I cannot ask a user to change these settings for our application.



  • You could add to your message.



    To avoid seeing this message again, you can set the log to overwrite...

  • Question...



    Why show the first question with the condition that answering 'Yes' will only work if you are administrator if you determine if this will work just after that?



    Wouldn't it be more user friendly to just check if the user is administrator first and then ask either the "Do you want me to clear that log for you?" question or show the "Please manually clear the log while logged on as administrator."...?



    P.S. I always remove the default import of the Microsoft.VisualBasic namespace because I don't like using VB6-compatibility code without being explicit. So I would never use vbCr. I prefer something like String.Format("Line 1{0}Line 2{0}{0}Are you sure?", Environment.Newline).

  • I think you will find something like the code below easier than fidgetting around with the current thread principal just to check if the current windows user has administrator rights:





    Imports System.Security.Principal



    Public Function IsAdministrator() As Boolean

    Dim winUser As WindowsIdentity = WindowsIdentity.GetCurrent()

    Dim winRights As New WindowsPrincipal(winUser)

    Return winRights.IsInRole(WindowsBuiltInRole.Administrator)

    End Function





    In my current project your code could backfire terribly because we have a custom written principal that is crucial to the security subsystem in the application and your code is setting the principal policy for the current applica

Comments have been disabled for this content.