简体   繁体   English

如何在F#Fable中登录Application Insights?

[英]How to log to Application Insights in F# Fable?

I am developing a SAFE application in F#, and part of it is sending some logs to Application Insights in Azure. 我正在使用F#开发一个SAFE应用程序,其中一部分是将一些日志发送到Azure中的Application Insights。 Server side logging is somewhat easy : 服务器端日志记录有点简单

open Microsoft.ApplicationInsights
open Microsoft.ApplicationInsights.Extensibility

let getTelemetryClient() =
    let key = <key>
    TelemetryConfiguration.Active.InstrumentationKey <- key
    TelemetryClient()

let log (message : string) =
    let logger = (lazy getTelemetryClient()).Value
    logger.TrackTrace message
    logger.Flush()

Now I want to do something similar in the client side, in Fable. 现在我想在Fable的客户端做类似的事情。 I have an AI key and I somehow need to send the data to Azure. 我有一个AI密钥,我不知何故需要将数据发送到Azure。 The same code does not transpile to JS. 相同的代码不会转化为JS。

This can be achieved by means of dynamic programming in Fable. 这可以通过Fable中的动态编程来实现。

  1. Add AI JS dependency in package.json : package.json添加AI JS依赖项:
{
    ...
    "devDependencies": {
        ...
        "applicationinsights-js": "^1.0.20"
    }
}
  1. Reflect it in webpack.config.js : webpack.config.js反映出来:
fsharpEntry: {
    "app": [
         ...
        "applicationinsights-js"
    ]
}
  1. Set up logging (call this in the init phase of the app): 设置日志记录(在应用程序的初始阶段调用它):
open Fable.Core.JsInterop
open Fable.Import.Browser

let setup() = 
    let key = <key>
    let config = createObj [ "instrumentationKey" ==> key ]
    window?appInsights?downloadAndSetup config
  1. Log like this: 像这样记录:
let log message = window?appInsights?trackTrace message

Maybe there are more adequate and/or type-safe ways, yet this works. 也许有更充分和/或类型安全的方式,但这是有效的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM