在 Azure 函数中使用关联 ID 进行日志记录

时间:2021-07-14 08:21:31

标签: c# azure .net-core azure-functions azure-application-insights

任何人都可以建议我如何实现添加相关 id 以跟踪每个 http 请求到以下函数的可追溯性。

import csv
from textblob import TextBlob
import string

z = 10
poscounter = 0
negcounter = 0
neucounter = 0
totalsentences = 0


outfile = open('/Users/first_lastname/Desktop/WebScraping/TripAdvisor/Juneau/analyzedData.csv', 'w')

with open('/Users/first_lastname/Desktop/WebScraping/TripAdvisor/Juneau/tripadvisor_2021.csv', 'r') as infile:
    reader = csv.reader(infile)
    header = next(reader) #converts each row to a list
    #[rows][columns]

    for row in reader:
        trip_review = row[2]
        #print(trip_review)
        y = trip_review.lower()
        y = y.translate(str.maketrans('', '', string.punctuation))
        y1 = TextBlob(y)
        trip_sentiment = y1.sentiment.polarity
        xplitIt = y.split(" ")
        for something in xplitIt:
            if something == "crowded" or something == "busy" or something == "crowds" or something == "hate" or something == "hated":
                tripSentiment = -0.1
                break
            
        if trip_sentiment ==0:
            neucounter+=1
                
        elif trip_sentiment >0 and trip_sentiment <=1:
            poscounter+=1

        elif trip_sentiment == -0.1 or trip_sentiment < 0:
            negcounter+=1
            
        totalsentences = totalsentences + 1    
        line = "{};{}\n".format(trip_review, trip_sentiment)
        outfile.write(line)
outfile.close()

print(f"Positive Sentiment: {(poscounter/totalsentences) * 100} %")
print(f"Negative Sentiment: {(negcounter/totalsentences) * 100} %")
print(f"Neutral Sentiment: {(neucounter/totalsentences) * 100} %")
#print(totalsentences)

1 个答案:

答案 0 :(得分:2)

所以,如果我没记错的话,您想要的是在出现异常的情况下,您可以在正文中返回 *something*,以便您可以在 Application Insights 中找到这种情况?如果是这样,试试这个:

var requestTelemetry = req.HttpContext.Features.Get<RequestTelemetry>();
var operationId = requestTelemetry.Context.Operation.Id;

并将 operationId 作为结果的一部分发送。

在 Application Insights 中,operationId 可用于跟踪从请求到异常的整个操作。