narratr/story

Story not stopped upon first MVC request

Opened this issue · 3 comments

Hello,

I'm experiencing some odd behaviour in an MVC application in ASP.NET 4.6.1 with OWIN.

I'm starting a story in the homepage like so:

public async Task<ActionResult> Index()
{
    return await Storytelling.Factory.StartNewAsync("Index", async () =>
    {
        await Task.FromResult(0);
        return View();
    });
}

I've registered the OwinMiddleware in your example, so all stories are under the context of a "Request" story. I'm registering the middleware like so:

app.Use(typeof(StoryMiddleware));

I've also registered a story handler for NLog:

public class NLogStoryHandler : StoryHandlerBase
    {
        private readonly IStoryFormatter storyFormatter;
        private static NLog.Logger logger = LogManager.GetCurrentClassLogger();

        public NLogStoryHandler(string name, IStoryFormatter storyFormatter = null)
            : base(name)
        {
            this.storyFormatter = storyFormatter ?? StoryFormatters.GetBasicStoryFormatter();
        }

        public override void OnStop(IStory story)
        {
            string str = this.storyFormatter.FormatStory(story, this.Name);
            logger.Info(str);
        }
    }

Finally, I'm setting up the factory like this:

Storytelling.Factory = new FileBasedStoryFactory(ConfigurationManager.AppSettings["StoryRulesetPath"],
                () => new object[] {
                    new NLogStoryHandler("NLogHandler")
                });

What is happening is that the first request made to ASP.NET is only logging the "Request" story. I put a breakpoint on the OnStop method, and the story should contain the "Index" story as a child, but it doesn't.
After the first request, the OnStop method is called twice. For both, the story is "Request", and one of them contains the "Request/Index" story as a child, but the second time it has no children.

I've included a sample so you can try it out for yourself. The log is being written under the logs folder, and you have to change the path in web.config for the RuleSet(is there a way to provide a relative path?).

WebApplication9.zip

I'd appreciate any insight into this problem.

Thanks

I looked into it a bit more, and it seems the reason those "Request" stories exist is because of the browser link feature VS has, which is sending those requests. But the first request is still not being logged as a structured story.

We'll take a look into this.

Hi @amitapl ,

Could you come up with any solutions to the above thread?
Thanks,