log4net 和“with try” F#

发布于 2025-01-08 12:00:21 字数 296 浏览 1 评论 0原文

我正在尝试使用 log4net 表达,但它不起作用。我认为缺少了一些东西,但我不知道是什么。 这是我的代码:

let clusterIDArray = try
                         myfunction
                     with
                         log.Fatal("my function is not working")   


log.Debug("my function is working")

有什么想法吗?

I am using log4net in a try with expression and it is not working. I think something is missing but I don't know what.
Here is my code :

let clusterIDArray = try
                         myfunction
                     with
                         log.Fatal("my function is not working")   


log.Debug("my function is working")

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

櫻之舞 2025-01-15 12:00:21

您的代码片段中有两个错误:

  • 您没有执行您的函数。我想你确实在真实的代码中执行了它;这是编造一个例子的问题。
  • 您没有捕获异常,或者您的 try...with... 块在语法上不正确。

假设我们在任何情况下捕获异常并返回一个数组,更惯用的日志记录方式可能是:

let clusterIDArray =
    try
       let result = myfunction args // Assume args are arguments declared before
       log.Debug("my function is working")
       result
    with 
    | ex ->
       // ex is of type exception which can be parsed by log4net
       log.Fatal("my function is not working: ", ex)   
       [||]

There are two mistakes in your code snippet:

  • You didn't execute your function. I suppose that you did execute it in your real code; it's the matter of making up an example.
  • You didn't catch exceptions or your try...with... block is not syntactically correct.

Assuming that we catch exceptions and return an array in any case, a more idiomatic way of logging could be:

let clusterIDArray =
    try
       let result = myfunction args // Assume args are arguments declared before
       log.Debug("my function is working")
       result
    with 
    | ex ->
       // ex is of type exception which can be parsed by log4net
       log.Fatal("my function is not working: ", ex)   
       [||]
怼怹恏 2025-01-15 12:00:21

如果不知道 myfunction 是什么,就很难说,但在您编写代码时,代码实际上并没有运行该函数。它只是将其作为值返回(从 try .. with 块)并将其分配给 clusterIDArray 值,因此代码永远不会抛出异常。

如果函数只接受一个 unit 参数,您可以在 try .. with 块内调用它,如下所示(如果它接受一些参数,您需要给它们块内的函数):

let clusterIDarray = 
  try 
    myfunction ()
  with _ ->
    log.Fatal("not working")
    reraise() // You still need to return something here or rethrow the exception

log.Debug("my function is working")  

编辑 正如其他人指出的,语法 with e 无效。如果你想忽略异常,你需要编写 with e ->with _ ->

It is hard to say without knowing what the myfunction is, but as you wrote it, the code does not actually run the function. It simply returns it as a value (from the try .. with block) and assigns it to the clusterIDArray value, so the code will never throw.

If the function takes just a unit argument, you can call it inside the try .. with block like this (if it takes some arguments, you'll need to give them to the function inside the block):

let clusterIDarray = 
  try 
    myfunction ()
  with _ ->
    log.Fatal("not working")
    reraise() // You still need to return something here or rethrow the exception

log.Debug("my function is working")  

EDIT As others noted, the syntax with e is not valid. You need to write with e -> or with _ -> if you want to ignore the exception.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文