Page 1 of 1

REST API to the Event log

PostPosted: Mon Jun 28, 2021 6:43 pm
by SBarnes
Will the self hosted api write to the event log with some code added such as the below provided the user the service is running under has the correct permissions.



Code: Select all
public void LogToEventLog(string Message, System.Diagnostics.EventLogEntryType EventLogEntryType)
{
    System.Diagnostics.EventLog Log = new System.Diagnostics.EventLog("Application");
    Log.Source = string.Format("Jiwa Plugin : {0}", "REST API");
    Log.WriteEntry(Message, EventLogEntryType);
    Log.Close();
}


Re: REST API to the Event log

PostPosted: Tue Jun 29, 2021 1:11 pm
by Mike.Sheen
SBarnes wrote:Will the self hosted api write to the event log with some code added such as the below provided the user the service is running under has the correct permissions.



Code: Select all
public void LogToEventLog(string Message, System.Diagnostics.EventLogEntryType EventLogEntryType)
{
    System.Diagnostics.EventLog Log = new System.Diagnostics.EventLog("Application");
    Log.Source = string.Format("Jiwa Plugin : {0}", "REST API");
    Log.WriteEntry(Message, EventLogEntryType);
    Log.Close();
}



Yes, that should work, but I'd use a different source so your logging isn't wrapped in ugly "The description for Event ID %n from source Application cannot be found" errors. This blog post has a good write-up on it - the executive summary is to use source of ".NET Runtime" and event id of 1000.

So, if you've imported the necessary namespace, this should be all you need:
Code: Select all
EventLog.WriteEntry(".NET Runtime", "this is my log message", EventLogEntryType.Warning, 1000);


When we do install Jiwa we do try to register ourselves as a source (which is one of the reasons administrator permissions are required to install) - but we don't create our own Event Message File - so I think that's why our logging to the event log has always been incorrect. We have that logged already as DEV-8736.

Re: REST API to the Event log  Topic is solved

PostPosted: Thu Jul 01, 2021 7:41 pm
by SBarnes
For anyone's benefit the following static class did the trick and given the client runs multiple copies of the api, test, staging and production this will include the database in the source.



Code: Select all
   public static class APIEventLogger
   {
      public static void LogToEventLog( string Message, System.Diagnostics.EventLogEntryType EventLogEntryType = System.Diagnostics.EventLogEntryType.Information)
      {
         if(! RESTAPIPlugin.WebhooksLog)
         {
            return;
         }
         try   
         {
            string DatabaseName  = "";
            try
            {
               string constr = RESTAPIPlugin.AppHost.GetDbConnection().ConnectionString;
               System.Data.SqlClient.SqlConnectionStringBuilder builder = new System.Data.SqlClient.SqlConnectionStringBuilder(constr);
               DatabaseName = builder["Initial Catalog"] as string;
            }
            catch(Exception ex1)
            {
               DatabaseName  = "";
            }
            
             System.Diagnostics.EventLog Log = new System.Diagnostics.EventLog("Application");
             Log.Source = string.Format("Jiwa Plugin : {0}", "REST API " + DatabaseName);
             Log.WriteEntry(Message, EventLogEntryType);
             Log.Close();      
         }
         catch(Exception ex)
         {
         }
      }

   }