gpt4 book ai didi

c# - 我在程序集 "Cannot serialized the aspects: Type ' 中得到 'log4net...' log4net.Core.LogImpl' - 我怎样才能让它序列化?

转载 作者:太空狗 更新时间:2023-10-29 22:22:59 27 4
gpt4 key购买 nike

环境

  • Windows 7 x64
  • 已安装 PostSharp 2.1.7.30
  • PostSharp 2.1.7.29 (从 PostSharp 安装目录中检索到的引用)
  • log4net 1.2.11.0 (从 log4net 二进制文件下载的 net\2.0\release 目录中检索到的引用)
  • ASP.NET 2.0 应用程序
  • Visual Studio 2010 SP1

看点

[Serializable]
public class MethodBoundaryAspect : PostSharp.Aspects.OnMethodBoundaryAspect
{
ILog _logger = LogManager.GetLogger("MethodBoundaryAspect");

public override void OnEntry(PostSharp.Aspects.MethodExecutionArgs args)
{
_logger.DebugFormat("The user {0} entered the method {1} at {2}.",
HttpContext.Current.Profile.UserName,
args.Method.Name,
DateTime.Now);

base.OnEntry(args);
}

public override void OnExit(PostSharp.Aspects.MethodExecutionArgs args)
{
_logger.DebugFormat("The user {0} exited the method {1} at {2}.",
HttpContext.Current.Profile.UserName,
args.Method.Name,
DateTime.Now);

base.OnExit(args);
}

public override void OnException(PostSharp.Aspects.MethodExecutionArgs args)
{
_logger.DebugFormat("The user {0} was executing the method {1} when an unhandled exception occurred at {2}.{3}{4}",
HttpContext.Current.Profile.UserName,
args.Method.Name,
DateTime.Now,
Environment.NewLine,
args.Exception.ToString());

base.OnException(args);
}
}

编译错误

Cannot serialize the aspects: Type 'log4net.Core.LogImpl' in Assembly 'log4net, Version=1.2.11.0, Culture=neutral, PublicKeyToken=669e0ddf0bb1aa2a' is not marked as serializable.

如何使 log4net.Core.LogImpl 可序列化以便编译此解决方案?

最佳答案

根据 Grant 和 mtsiakiris 的评论,我通过向字段添加 [NonSerialized] 属性解决了这个问题,如下所示:

[NonSerialized]
ILog _logger = LogManager.GetLogger("MethodBoundaryAspect");

然后在序列化过程中忽略它。发生这种情况的原因是因为 PostSharp 需要在其 IL 生成期间序列化该属性。

关于c# - 我在程序集 "Cannot serialized the aspects: Type ' 中得到 'log4net...' log4net.Core.LogImpl' - 我怎样才能让它序列化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16239065/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com