REST API to the Event log  Topic is solved

Discussions relating to the REST API of Jiwa 7.

REST API to the Event log

Postby SBarnes » Mon Jun 28, 2021 6:43 pm

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();
}

Regards
Stuart Barnes
SBarnes
Shihan
Shihan
 
Posts: 1619
Joined: Fri Aug 15, 2008 3:27 pm
Topics Solved: 175

Re: REST API to the Event log

Postby Mike.Sheen » Tue Jun 29, 2021 1:11 pm

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.
Mike Sheen
Chief Software Engineer
Jiwa Financials

If I do answer your question to your satisfaction, please mark it as the post solving the topic so others with the same issue can readily identify the solution
User avatar
Mike.Sheen
Overflow Error
Overflow Error
 
Posts: 2445
Joined: Tue Feb 12, 2008 11:12 am
Location: Perth, Republic of Western Australia
Topics Solved: 757

Re: REST API to the Event log  Topic is solved

Postby SBarnes » Thu Jul 01, 2021 7:41 pm

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)
         {
         }
      }

   }
Regards
Stuart Barnes
SBarnes
Shihan
Shihan
 
Posts: 1619
Joined: Fri Aug 15, 2008 3:27 pm
Topics Solved: 175


Return to REST API

Who is online

Users browsing this forum: No registered users and 1 guest

cron