[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 711
  • Last Modified:

event log - EventLogEntryType

How do I set the EventLogEntryType based on the type of error?

I tried the following but am getting errors:




if (WhoIsEligible(MyUserID) == false)
 {
  //functionName, MyErrorContent, case
  LogErrorWriter("WhoIsEligible", "User: " + MyUserID + " is not eligible for the upgrade.", 3);
  //redirect to error page
  Server.Transfer("error.aspx");
 }


protected void LogErrorWriter(String FunctionName, String MyErrorContent, int32 intEV)
 {
  if (!EventLog.SourceExists("Intranet.AppName")) //
   {
      EventLog.CreateEventSource(FunctionName, "Intranet.AppName");
   }
   //determine which event log entry type
    switch (intEV)
     {
      case 1:
       myerrornow = "Error";
       break;
      case 2:
       myerrornow = FailureAudit;
       break;
      case 3:
       myerrornow = Information;
       break;
      case 4:
       myerrornow = SuccessAudit;
       break;
      case 5:
       myerrornow = Warning;
       break;
     }
  //write to log
  EventLog log = new EventLog("Intranet.AppName");
  log.Source = "MyDeviceUpgrade." + FunctionName;
  log.WriteEntry(MyErrorContent, EventLogEntryType.myerrornow);
 }
0
fwsteal
Asked:
fwsteal
  • 3
  • 2
1 Solution
 
vo1dCommented:
change your switch statement like this:


EventLogEntryType type;            
switch (intEV)
{
case 1:
     type = EventLogEntryType.Error;
     break;
case 2:
     type = EventLogEntryType.FailureAudit;
     break;
case 3:
     type = EventLogEntryType.Information;
     break;
case 4:
     type = EventLogEntryType.SuccessAudit;
     break;
case 5:
     type = EventLogEntryType.Warning;
     break;
}

change your writeentry line like this:
log.WriteEntry(MyErrorContent, type);

0
 
fwstealAuthor Commented:
i did the following and got an error:


if (WhoIsEligible(MyUserID) == false)
 {
  //functionName, MyErrorContent, case
  LogErrorWriter("WhoIsEligible", "User: " + MyUserID + " is not eligible for the upgrade.", 3);
  //redirect to error page
  Server.Transfer("error.aspx");
 }


protected void LogErrorWriter(String FunctionName, String MyErrorContent, Int32 intEV)
 {
  if (!EventLog.SourceExists("Intranet.AppName")) //
   {
      EventLog.CreateEventSource(FunctionName, "Intranet.AppName");
   }
   //determine which event log entry type
    EventLogEntryType type;            
    switch (intEV)
     {
      case 1:
      type = EventLogEntryType.Error;
      break;
      case 2:
      type = EventLogEntryType.FailureAudit;
      break;
      case 3:
      type = EventLogEntryType.Information;
      break;
      case 4:
      type = EventLogEntryType.SuccessAudit;
      break;
      case 5:
      type = EventLogEntryType.Warning;
      break;
     }
  //write to log
  EventLog log = new EventLog("Intranet.AppName");
  log.Source = "MyDeviceUpgrade." + FunctionName;
  log.WriteEntry(MyErrorContent, type);
  //log.WriteEntry(MyErrorContent, EventLogEntryType.myerrornow);
 }



Error      Use of unassigned local variable 'type'
0
 
vo1dCommented:
interessting, that works under 2.0.
try changing the line
EventLogEntryType type;
to this:

EventLogEntryType type = EventLogEntryType.Error;

does that work?
0
 
fwstealAuthor Commented:
if used as above I get the following:

A local variable named 'type' is already defined in this scope --
case 1:
                //type = EventLogEntryType.Error;
                EventLogEntryType type = EventLogEntryType.Error;
                break;
            case 2:
                //type = EventLogEntryType.FailureAudit;
                EventLogEntryType type = EventLogEntryType.FailureAudit;
                break;
            case 3:
                //type = EventLogEntryType.Information;
                EventLogEntryType type = EventLogEntryType.Information;
                break;
            case 4:
                //type = EventLogEntryType.SuccessAudit;
                EventLogEntryType type = EventLogEntryType.SuccessAudit;
                break;
            case 5:
                //type = EventLogEntryType.Warning;
                EventLogEntryType type = EventLogEntryType.Warning;
                break;

The name 'type' does not exist in the current context --- log.WriteEntry(MyErrorContent, type);
0
 
fwstealAuthor Commented:
duh; my error on the above
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now