如何使用程序集名称命名 log4net 输出文件?

发布于 2024-10-11 13:53:46 字数 253 浏览 8 评论 0原文

是否有任何变量可以用来命名我的日志文件?

<file value="${ALLUSERSPROFILE}\${AssemblyName}.log.xml" />

的地方

  • ${ALLUSERSPROFILE} 真正起作用
  • ${AssemblyName} 不起作用,这是我为了说明我想要的而编造的东西。

Is there any variable which I could use to name my log files?

<file value="${ALLUSERSPROFILE}\${AssemblyName}.log.xml" />

Where

  • ${ALLUSERSPROFILE} really works
  • ${AssemblyName} doesn't, it is something I made up just for illustration of what I want.

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

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

发布评论

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

评论(5

爱的那么颓废 2024-10-18 13:53:46

正如其他人所说,程序集名称没有内置占位符,并且有几种方法可以实现它。另一种方法是向 Log4Net 框架注册您自己的处理程序/转换器。

基本上,您执行与 log4net.Util.PatternString< 相同的操作/a> 类在内部执行(您可以检查相关源代码以获得比下面给出的片段更完整的“示例”)。

示例:

<file value="[ASSEMBLYNAME].log" type="MyExpressionHandler, MyAssembly"/>

然后代码:

using log4net.Core;

public sealed class MyExpressionHandler : IOptionHandler
{
   private string m_str;

   public MyExpressionHandler(string str)
   {
      m_str = str;
   }

   public void ActivateOptions()
   {
   }

   public string Format()
   {
      return m_str.Replace("[ASSEMBLYNAME]", /* ... whatever ... */);
   }
}

然后提供匹配的“Converter Class”。

   internal class MyExpressionHandlerConverter : IConvertTo, IConvertFrom
    {
        public bool CanConvertTo(Type targetType)
        {
            return (typeof(string).IsAssignableFrom(targetType));
        }

        public object ConvertTo(object source, Type targetType)
        {
            MyExpression patternString = source as MyExpression;
            if (patternString != null && CanConvertTo(targetType))
            {
                return patternString.Format();
            }
            throw ConversionNotSupportedException.Create(targetType, source);
        }

        public bool CanConvertFrom(System.Type sourceType)
        {
            return (sourceType == typeof(string));
        }

        public object ConvertFrom(object source)
        {
            string str = source as string;
            if (str != null)
            {
                return new MyExpression(str);
            }
            throw ConversionNotSupportedException.Create(typeof(MyExpression), source);
        }
}

最后让 Log4Net 框架知道您的转换器:

ConverterRegistry.AddConverter(typeof(MyExpression), typeof(MyExpressionConverter));

As others said, there is no builtin placeholder for the assemblyname and a couple of ways to achieve it anyhow. Another one is to register your own handler/converter with the Log4Net framework.

Basically, you do the same that the log4net.Util.PatternString class does internally (and you might check the relevant source code for a more complete "example" than the fragements given below).

Example:

<file value="[ASSEMBLYNAME].log" type="MyExpressionHandler, MyAssembly"/>

Then the code:

using log4net.Core;

public sealed class MyExpressionHandler : IOptionHandler
{
   private string m_str;

   public MyExpressionHandler(string str)
   {
      m_str = str;
   }

   public void ActivateOptions()
   {
   }

   public string Format()
   {
      return m_str.Replace("[ASSEMBLYNAME]", /* ... whatever ... */);
   }
}

Then provide a matching "Converter Class".

   internal class MyExpressionHandlerConverter : IConvertTo, IConvertFrom
    {
        public bool CanConvertTo(Type targetType)
        {
            return (typeof(string).IsAssignableFrom(targetType));
        }

        public object ConvertTo(object source, Type targetType)
        {
            MyExpression patternString = source as MyExpression;
            if (patternString != null && CanConvertTo(targetType))
            {
                return patternString.Format();
            }
            throw ConversionNotSupportedException.Create(targetType, source);
        }

        public bool CanConvertFrom(System.Type sourceType)
        {
            return (sourceType == typeof(string));
        }

        public object ConvertFrom(object source)
        {
            string str = source as string;
            if (str != null)
            {
                return new MyExpression(str);
            }
            throw ConversionNotSupportedException.Create(typeof(MyExpression), source);
        }
}

And finally make your converter known to the Log4Net framework:

ConverterRegistry.AddConverter(typeof(MyExpression), typeof(MyExpressionConverter));
小…楫夜泊 2024-10-18 13:53:46

默认情况下不是。

此语法用于扩展Windows 环境变量。虽然 ALLUSERSPROFILE 是标准环境变量,但 AssemblyName 不是。您必须自己设置AssemblyName,这作为动态解决方案并不容易\不可能。

Not by default.

This syntax is for expanding Windows Environment Variables. Whilst ALLUSERSPROFILE is a standard environment variable, AssemblyName is not. You would have to set AssemblyName yourself, which will not be easy\possible as a dynamic solution.

难以启齿的温柔 2024-10-18 13:53:46

NLog 中的 processname 类似于 log4net 中的 PatternLayout ;它们都是日志本身布局/渲染的一部分。

如果您想在应用程序中使用环境变量,可以在安装过程中强制创建环境变量,也可以在代码中创建自己的环境变量; 此处有一个 C# 示例。

如果该选项不适合您的需求,那么您可以动态设置路径,如下所示 这里

processname within NLog is akin to the PatternLayout within log4net; they are both part of the layout/rendering of the logs themselves.

If you want to use an environment variable within your application either force the creation as part of the install or create your own within your code; a C# example is here.

If that option does not suit your needs then you can dynamically set the path as shown here.

梦断已成空 2024-10-18 13:53:46

您可以编写自己的图案布局转换器。我发布了一个示例 这里,我认为很容易修改它,以便它写入程序集名称......

You could write your own pattern layout converter. I posted a sample here and I think it is easy to modify that so that it writes the assembly name...

梦在深巷 2024-10-18 13:53:46

另一种选择是,在初始化时定义一个全局属性:

GlobalContext.Properties["pid"] = System.Diagnostics.Process.GetCurrentProcess().Id;
this.logger = log4net.GetLogger("LoggerSample");

在您的配置文件中,您可以使用“pid”属性,如下所示:

<param name="ConversionPattern" value="%d;%property{log4net:HostName};%property{pid};%t;%p;%c;%M;%L;%m%n" />

希望,我很清楚!

br++马布拉

Another option is, to define a global property at initialization time:

GlobalContext.Properties["pid"] = System.Diagnostics.Process.GetCurrentProcess().Id;
this.logger = log4net.GetLogger("LoggerSample");

In your configuration file, you can use the "pid" property like this:

<param name="ConversionPattern" value="%d;%property{log4net:HostName};%property{pid};%t;%p;%c;%M;%L;%m%n" />

Hope, I was clear!

br++mabra

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