SaturnFramework / Saturn

Opinionated, web development framework for F# which implements the server-side, functional MVC pattern
https://saturnframework.org
MIT License
708 stars 108 forks source link

Logging does not work with Saturn AzureFunctions #199

Open toburger opened 4 years ago

toburger commented 4 years ago

Setting the logger via the azureFunction computation expression and then getting it with ctx.GetLogger() doesn't actually log anything.

How to reproduce: Take the Saturn.Func sample from this repo and update the source code of SaturnFunc.fs and run the function app:

namespace MyFunctions

open Microsoft.AspNetCore.Http
open Microsoft.Azure.WebJobs.Host
open Saturn.AzureFunctions
open Microsoft.Extensions.Logging
open Giraffe

module Functions =
    open Saturn
    open Microsoft.Azure.WebJobs

    let testCntl = controller {
        index (fun ctx ->
            // Get logger instance
            let logger = ctx.GetLogger()
            // This warning doesn't get logged
            logger.LogWarning("logging doesn't work")
            Controller.text ctx "Hello world")
        show (fun ctx id -> id |> sprintf "Hello world, %s" |> Controller.text ctx)
    }

    let func log = azureFunction {
        host_prefix "/api"
        use_router testCntl
        logger log
    }

    [<FunctionName("HelloWorld")>]
    let helloWorld ([<HttpTrigger(Extensions.Http.AuthorizationLevel.Anonymous, Route = "{route?}")>]req: HttpRequest, log: ILogger) =
        // This warning actually gets logged
        log.LogWarning("logging does work")
        func log req

You can see, that the warning inside the helloWorld function gets printed, while the logger of the index route doesn't.