3

I'm trying to do some logging on my service fabric web api project and it's always disabled. I've never seen this behavior before with any other SF project type.

[NonEvent]
    public void ServiceMessage(ServiceContext serviceContext, string message, params object[] args)
    {
        if (IsEnabled())
        {
            string finalMessage = string.Format(message, args);

            ServiceMessage(
                serviceContext.ServiceName.ToString(),
                serviceContext.ServiceTypeName,
                GetReplicaOrInstanceId(serviceContext),
                serviceContext.PartitionId,
                serviceContext.CodePackageActivationContext.ApplicationName,
                serviceContext.CodePackageActivationContext.ApplicationTypeName,
                serviceContext.NodeContext.NodeName,
                finalMessage);
        }
    }

IsEnabled always returns false. Is there a special setup you need to do for Web api projects? This is .net core as well. Not sure if that is effecting it.

g.t.w.d
  • 601
  • 1
  • 10
  • 30
  • Not that I am aware of. Did you make any alterations to the EventSource class? – Peter Bons May 25 '17 at 19:51
  • Completely untouched except for the EventSource attribute name string. But I change that in all my Service Fabric projects. This .net core web api SF project is the only time I've ever seen this behavior. I feel like I might be missing something in my startup or when my weblistener is being created or something to that effect. – g.t.w.d May 25 '17 at 19:58
  • What name did you give this one? Looking at one of my own web api stateless services there is no setup required whatsoever hence the question. Maybe it is an invalid one or something. – Peter Bons May 25 '17 at 20:04
  • It's just a string. It's the same naming convention I use for all other SF project ServiceEventSources. I guess I need to figure how that IsEnabled gets set. That's from EventSouce base class. Do you have anything special in your startup or anything? – g.t.w.d May 25 '17 at 20:36
  • No nothing, you can see it for yourself at [my repo](https://github.com/Expecho/Service-Fabric-Actors-Playground/tree/master/src/WebApi) . I know EventSources can be picky about parameters and the such, but if there are no modifications. In .Net full there is a way to test them, see my answer for [this question](https://stackoverflow.com/questions/37561974/semantic-logging-isenabled-si-always-false). But that probably won't work for ASF sources due to the references. Although, you can always try. – Peter Bons May 25 '17 at 20:40

1 Answers1

1

It is not depend of SF project type. Find out the name of your EventSource:

[EventSource(Name = "MyCompany-SFApp-Stateful1")]
internal sealed class ServiceEventSource : EventSource

In this case it is MyCompany-SFApp-Stateful1. Then you need to add this name as a ETW provider in Diagnostic Events tab (View - Other Windows - Diagnostic Events)

enter image description here

Roman Marusyk
  • 23,328
  • 24
  • 73
  • 116
  • I'm stepping through the code and that call to IsEnabled returns false. Which is from the base EventSource class. So no ETW message is every being created. – g.t.w.d May 25 '17 at 20:37
  • @g.t.w.d It is because you need to enable event source, add it to providers. Did you do that? Add. press Apply and rebuild. After that IsEnabled will be true – Roman Marusyk May 25 '17 at 20:54
  • I'll try. I've never had to do that ever before with any other SF project type. – g.t.w.d May 25 '17 at 21:21
  • @g.t.w.d I had the same problem. This solution was helpful for me. Please let me know the result – Roman Marusyk May 25 '17 at 21:23
  • I've tried to add it three times, but it never saves it. – g.t.w.d May 25 '17 at 21:26
  • What do you mean by "never saves it"? Press F5 then add ETV and apply? – Roman Marusyk May 25 '17 at 21:28
  • Yes. I did that. And then once I stop debugging, it's gone. However, my other SF projects in this solution are always in that list. – g.t.w.d May 25 '17 at 21:29