cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - Visit the PTCooler (the community lounge) to get to know your fellow community members and check out some of Dale's Friday Humor posts! X

Get name of service inside service

jensc
17-Peridot

Get name of service inside service

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.

1 ACCEPTED SOLUTION

Accepted Solutions
jensc
17-Peridot
(To: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.

View solution in original post

14 REPLIES 14
tdixit
13-Aquamarine
(To:jensc)

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

jensc
17-Peridot
(To:tdixit)

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.

odukiya
15-Moonstone
(To:jensc)

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 ?

jensc
17-Peridot
(To:odukiya)

Hello,

 

I am adding logs to all of my services, and while writing different: "logger.info("MyServiceName") for each service is fine, it would have been easier to be able to use some variable instead of the string, like:

"logger.info(MyServiceNameVariable)"

This way I wouldn't run into a situation where I might have misspelled something and I can also just copy-paste the line of code everywhere I'd want it.

 

As I mentioned in a previous post, there is a "me.name" which gives the name of the thing that the service is in. 

If something similar would have been available for the service name it would have been great.

tdixit
13-Aquamarine
(To:jensc)

Hello @jensc 

 

I am not sure if we ca retrieve service name like me.Name for a thing but if it helps you can try retrieving your service name through below code

 

var result1= me.GetServiceDefinition({
name: "Your  Service Name" /* STRING */
});

result=result1.name;

 

When you use the above code ,it will give you the service name in the result only when you type the correct service name in the code otherwise it will give an error

 

Regards,

Toolika Dixit

 

odukiya
15-Moonstone
(To:jensc)

Hello @jensc 

 

So if this is the case than may be you can create a property in the thing by name MyServiceName.

Now in the service using me.MyServiceName='yourservicename' you can assign it different name as per the service and use that particular property in logger.info(me.MyServiceName).

 

Hope it helps, Let me know if you face any further challenges.

Thanks

Om DUkiya

jensc
17-Peridot
(To:odukiya)

Hello,

 

Yes, this is a possible way of doing it, but then I would still have to write down all of the service names, so I might as well just write them down as strings in the logger function.

 

It seems like there might not be any such function that I am looking for. Maybe they never developed that from the support article that I found.

 

But thank you for your help! 

bbeuckSIG
15-Moonstone
(To:jensc)

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.

odukiya
15-Moonstone
(To:bbeuckSIG)

Hello @bbeuckSIG 

 

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

 

Thanks

Om Dukiya

slangley
23-Emerald II
(To:bbeuckSIG)

Hi @bbeuckSIG.

 

You can submit your ThingWorx requests here.

 

Regards.

 

--Sharon

jensc
17-Peridot
(To: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.

slangley
23-Emerald II
(To:jensc)

Hi @jensc.

 

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

 

Regards.

 

--Sharon

jensc
17-Peridot
(To:slangley)

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.

nmilleson
17-Peridot
(To:jensc)

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! 

Top Tags