Skip to main content
17-Peridot
January 14, 2021
Solved

Get name of service inside service

  • January 14, 2021
  • 4 replies
  • 6410 views

Hello,

 

I am looking for a way to get the name of services within the services themselves.

 

Like if I wanted to write the name of the service in the log like this: "logger.info("serviceName");

I found an old support article that seems quite similar to what I am looking for: https://www.ptc.com/en/support/article/CS219740

 

But it seems like this might not have been implemented?

 

Does anyone have any ideas? I know I could just write the name of the service manually, but I might have several services where the same log will be present, and it would have been sweet if I didn't have to manually write the names.

Best answer by jensc

Thanks for all of your replies, 

 

I have asked a PTC resource about this although I have not received any response to it yet.

But I think it will probably be the same as you have all already said, to make a request to PTC for it.

 

So I will put this response as the solution for now and if I hear back from the PTC resource I'll change the solution.

4 replies

5-Regular Member
January 14, 2021

Hi @jensc 

 

Thank you for reaching out to us.

 

Please refer this community post  which might help you to achieve your use case.

 

Thanks & Regards,

Toolika Dixit

jensc17-PeridotAuthor
17-Peridot
January 14, 2021

Hello,

 

Thank you for your reply.

 

The post you mentioned is something I've already read, it does unfortunately not match my use case.

I would still need to know the names of the services.

 

What I was looking for is more similar to "me.name", which gives the name of the thing.

I thought the support article seemed very similar to that. But maybe I was wrong.

 

I'll also ask our PTC resource if he knows a way of doing this, but it is perhaps not possible.

16-Pearl
January 14, 2021

Hello @jensc 

 

 

There is a service by the name GetServiceDefinition which can provide you meta data related to any service you want. But i am confused regarding your use case here like you want name of same service you are trying to execute within that service? or you want to get name of all the services which are present in a thing. Can you please elaborate your use case here. What you are trying to do with the service name ?

16-Pearl
January 19, 2021

Same use case here, for the purpose of simple, aligned logging.

 

Additionally we have "renamed" (copied to new name and deleted old) several Services without cleaning up the code log entries which leads to confusion in ScriptLog.

 

Would be great to see this feature implemented.

16-Pearl
January 19, 2021

Hello @BennyB 

 

As this is important for you ,You can submit a product idea for an enhancement request of a PTC product.

 

Thanks

Om Dukiya

jensc17-PeridotAuthorAnswer
17-Peridot
January 20, 2021

Thanks for all of your replies, 

 

I have asked a PTC resource about this although I have not received any response to it yet.

But I think it will probably be the same as you have all already said, to make a request to PTC for it.

 

So I will put this response as the solution for now and if I hear back from the PTC resource I'll change the solution.

Support
January 29, 2021

Hi @jensc.

 

Have you received further information that you can share with the community on this post?

 

Regards.

 

--Sharon

jensc17-PeridotAuthor
17-Peridot
January 30, 2021

Hello,

 

Yes I have had a reply from our PTC resource and it does seem like this is unfortunately not possible with current functionality.

 

I suppose you could do as @nmilleson suggests, nesting exceptions into the code. Although for my use case (and I guess for many others, this doesn't really work as it would show up in scriptLog as an error when the event should really be logged as info for example.

 

If I remember correctly the PTC resource suggested to create a PTC request for this functionality. I might do it some day when I have more time, but for now I'll just type all service names manually.

17-Peridot
January 29, 2021

So I've found a strange way to get what you need... if you force a simple exception (I did it by trying to reference a non-existent variable), you can get the service name out of the error stack.  For instance:

 

try{
 var result = hello[0];
}
catch (err){
 var result = err.stack;
}

 

err.stack in this case will be just:

 

at myService:2

 

You should be able to parse that out of there pretty easily.  Not ideal, but it gets you there!