Community Tip - You can Bookmark boards, posts or articles that you'd like to access again easily! X
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.
Solved! Go to Solution.
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.
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.
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 ?
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.
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
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
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!
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.
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
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.
Hi @jensc.
Have you received further information that you can share with the community on this post?
Regards.
--Sharon
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.
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!