Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
659 views
in Technique[技术] by (71.8m points)

botframework - Log additional information with TranscriptLoggerMiddleware

I'm using TranscriptLoggerMiddleware to log transcript to Azure blobs. Now, I want to add additional information to the activity, for example, account ID.

Ideally I want the account ID to be the top level folder when creating the blobs so one can easily locate all conversations for a given account.

The logger is only passed the activity without any context. So I'm looking at the Entities activity property which I can potentially use for storing my account ID.

Is this a valid approach? Any other ideas on how to implement this?

question from:https://stackoverflow.com/questions/65949357/log-additional-information-with-transcriptloggermiddleware

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Answering my own question. This worked for me:

public class SetEntityMiddleware : IMiddleware
{
    private readonly BotState _userState;
    private readonly IStatePropertyAccessor<UserProfileState> _userProfileState;

    public SetEntityMiddleware(UserState userState)
    {
        _userState = userState;
        _userProfileState = userState.CreateProperty<UserProfileState>(nameof(UserProfileState));
    }

    public async Task OnTurnAsync(ITurnContext turnContext, NextDelegate next, CancellationToken cancellationToken = default)
    {
        var userProfile = await _userProfileState.GetAsync(turnContext, () => new UserProfileState(), cancellationToken);
        this.SetEntity(turnContext.Activity, userProfile);

        turnContext.OnSendActivities(async (ctx, activities, nextSend) =>
        {
            var userProfile = await _userProfileState.GetAsync(ctx, () => new UserProfileState(), cancellationToken);

            foreach (var activity in activities)
            {
                this.SetEntity(activity, userProfile);
            }

            return await nextSend().ConfigureAwait(false);
        });

        await next(cancellationToken).ConfigureAwait(false);
    }

    private void SetEntity(Activity activity, UserProfileState userProfile)
    {
        if (activity.Type == ActivityTypes.Message &&
            !string.IsNullOrEmpty(userProfile.AccountNumber))
        {
            var entity = new Entity();
            entity.Type = "userProfile";
            entity.Properties["accountNumber"] = userProfile.AccountNumber;

            if (activity.Entities == null)
            {
                activity.Entities = new List<Entity>();
            }

            activity.Entities.Add(entity);
        }
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...