log4net 和“with try” F#
我正在尝试使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的代码片段中有两个错误:
try...with...
块在语法上不正确。假设我们在任何情况下捕获异常并返回一个数组,更惯用的日志记录方式可能是:
There are two mistakes in your code snippet:
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:
如果不知道
myfunction
是什么,就很难说,但在您编写代码时,代码实际上并没有运行该函数。它只是将其作为值返回(从try .. with
块)并将其分配给clusterIDArray
值,因此代码永远不会抛出异常。如果函数只接受一个
unit
参数,您可以在try .. with
块内调用它,如下所示(如果它接受一些参数,您需要给它们块内的函数):编辑 正如其他人指出的,语法
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 thetry .. with
block) and assigns it to theclusterIDArray
value, so the code will never throw.If the function takes just a
unit
argument, you can call it inside thetry .. with
block like this (if it takes some arguments, you'll need to give them to the function inside the block):EDIT As others noted, the syntax
with e
is not valid. You need to writewith e ->
orwith _ ->
if you want to ignore the exception.