C#-Azure function:How to log message to Queue storage when calling to “HTTP Trigger” azure function?

久未见 提交于 2019-12-24 19:10:57

问题


I have a azure function with "HTTP Trigger" in C#. I want to log a message to queue storage when I invoke azure function with "HTTP trigger".

When "HTTP trigger" is invoked it logs a message (log.info("C# Http trigger....");) somewhere else but I want to this log message along with ipaddress, (when user makes request to "HTTP trigger" function) to be stored in Queue storage (log.info("C# Queue trigger ",ipaddress)) so that I can block client ipaddress for a day, if user makes calls to api beyond the number of attempts and next day again I can unblock client ipaddress by taking all ipaddress from queue storage running timer trigger in cloud.

Is there a possibility to achieve this or else Is there any alternate way to store ipaddress other than Queue storage?


回答1:


from what you wrote I would guess, that you want to offer some kind of API to your users and want to limit the access. To achieve this, I would rather suggest using Azure API Management (https://azure.microsoft.com/en-us/services/api-management/). It offers you a access quote, which limits the user's access to the API endpoint based on different criteria. It also offers much more: user management, payment, subscriptions, security and much more.




回答2:


For retrieving the client IP under your Azure function with HTTP Trigger, you could use the following code snippet:

run.csx:

#r "System.Web"

using System.Web;
using System.Net;

public static HttpResponseMessage Run(HttpRequestMessage req, TraceWriter log)
{   
    var address = ((HttpContextWrapper)req.Properties["MS_HttpContext"]).Request.UserHostAddress;
    log.Info($"Your client IP: {address}");
    return req.CreateResponse(HttpStatusCode.OK, $"Your client IP: {address}");
}

For more details, you could refer to this issue.

so that I can block client ipaddress for a day, if user makes calls to api beyond the number of attempts and next day again I can unblock client ipaddress by taking all ipaddress from queue storage running timer trigger in cloud.

I would prefer to use table storage for storing the access logs for the specific IPs. You could set the PartitionKey column to the Date, set RowKey to the ClientIP or ClientIP + ApiName, also you could add other columns (Timestamp, TotalRequests, etc). Also, you could refer to Azure Storage Table Design Guide for designing your storage table.

For your azure function, you could use Storage table bindings and Table Storage SDK for reading request log for the specific IP and update the total request count for the specific IP. Moreover, here is a similar blog, you could refer to here.

UPDATE:

You could configure the logs to be stored in the file system or blob storage under the "MONITORING > Diagnostic logs" as follows:

For Application Logging (Filesystem), you could find the logs via kudu:

For Application Logging (Blob), you could leverage the Storage Explorer to retrieve your logs.



来源:https://stackoverflow.com/questions/46717438/c-azure-functionhow-to-log-message-to-queue-storage-when-calling-to-http-trig

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!