C# 3.0 runtime macros and log4net

  • Thread starter Thread starter Arto Viitanen
  • Start date Start date
A

Arto Viitanen

Would it be possible to use C# 3.0 runtime macros to do following:

If Apache's log4net is loaded, calls like

log.DebugFormat("Hello {0}","world");

generate the logging, but if the dll is not loaded, nothing happens?
(I like to make program so, that the if the dll is missing, it works
but without the logging)
 
Arto Viitanen said:
Would it be possible to use C# 3.0 runtime macros to do following:

What do you mean by "runtime macros"? There aren't any such things as
far as I'm aware.
If Apache's log4net is loaded, calls like

log.DebugFormat("Hello {0}","world");

generate the logging, but if the dll is not loaded, nothing happens?
(I like to make program so, that the if the dll is missing, it works
but without the logging)

Do you mean partial methods? If so, they're not an execution-time
feature, they're a compile-time feature.
 
Jon Skeet [C# MVP] kirjoitti:
What do you mean by "runtime macros"? There aren't any such things as
far as I'm aware.
I found a web page talking about runtime macros. I guess what I was
asking is using the LINQ feature: compiler ejects AST to code, so the
program can evaluate the expression. So what I am asking is: is is
possible to make AST from

log.Debug("Hello world");

that will either call log4net's log object, or do nothing, based
on if the log4net is loaded or not.
 
Arto Viitanen said:
Jon Skeet [C# MVP] kirjoitti:
What do you mean by "runtime macros"? There aren't any such things as
far as I'm aware.

I found a web page talking about runtime macros.

Then that page is giving a misguided impression. There's nothing in C#
3 which corresponds to "runtime macros".
I guess what I was
asking is using the LINQ feature: compiler ejects AST to code, so the
program can evaluate the expression.

That sounds like you're talking about expression trees, but I can't see
how they would help you out here, I'm afraid.
So what I am asking is: is is
possible to make AST from

log.Debug("Hello world");

that will either call log4net's log object, or do nothing, based
on if the log4net is loaded or not.

Well, you could write a log class which tried to load log4net and
either swallowed all calls or forwarded them on, but it wouldn't need
any C# 3 features.
 
Arto said:
Would it be possible to use C# 3.0 runtime macros to do following:

If Apache's log4net is loaded, calls like

log.DebugFormat("Hello {0}","world");

generate the logging, but if the dll is not loaded, nothing happens?
(I like to make program so, that the if the dll is missing, it works
but without the logging)

I believe you will need to use reflection to accomplish that.

See example below.

Note that reflection carry a certain overhead.

Arne

====================================================

using System;
using System.IO;
using System.Reflection;

//using log4net;
//using log4net.Config;

namespace E
{
public class OptionalLog4netLogger
{
private Assembly asm;
private object il;
private MethodInfo mi;
public OptionalLog4netLogger(string name)
{
try
{
asm = Assembly.LoadFrom(@"C:\log4net.dll");
Type xc = asm.GetType("log4net.Config.XmlConfigurator",
false, false);
xc.InvokeMember("Configure", BindingFlags.DeclaredOnly
| BindingFlags.Public | BindingFlags.Static | BindingFlags.InvokeMethod,
null, null, null);
Type lm = asm.GetType("log4net.LogManager", false, false);
il = lm.InvokeMember("GetLogger",
BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Static |
BindingFlags.InvokeMethod, null, null, new object[] { name });
mi = il.GetType().GetMethod("DebugFormat",
BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Instance,
null, new Type[] { typeof(String), typeof(object[]) }, null);
}
catch(FileNotFoundException)
{
asm = null;
}
}
public void DebugFormat(string fmt, params object[] args)
{
if(asm != null)
{
mi.Invoke(il, new object[] { fmt, args});
}
}
}
public class Test
{
public static void Main(string[] args)
{
OptionalLog4netLogger log = new OptionalLog4netLogger("Test");
//XmlConfigurator.Configure();
//ILog log = LogManager.GetLogger("Test");
log.DebugFormat("This is just a {0} test", "little");
}
}
}
 
Back
Top