I have a c开发者_运维知识库onsole MEF application which convert files. There are some classes for logging warnings and errors, each in separate assembly. Every message, which I want to log, goes to LoggerProxy class and from it to all relevant loggers. I'm using strongly typed resource names, e.g. Message.NoFilesFound. Here is my problem - the strings for me must be in english or german, but messages for file owner must go in his language. It means that Message.NoFilesFound should be in german on console and in the log file, but e.g. italian in the mail for customer.
How can I setup the system for thist task?
Now I have:
class Main()
{
Message.Culture = new CultureInfo("it");
LoggerProxy.Write( Destination.Console|Destination.Customer, Message.NoFilesFound );
}
class LoggerProxy
{
[ImportMany]
Lazy<ILogger,ILoggerMetadata>[] Loggers { get; set; }
public void Write( Destination dest, string msg )
{
foreach ( var logger in Loggers )
if ( (logger.Metadata.Destination & dest ) != 0 )
logger.Value.Write( msg );
}
}
I would like to have the culture change in LoggerProxy.Write, but the parameter is already localized string.
I would like to say Log( Message.FileNotFound ) and have one line on console "Datei nicht gefunden", same line in logfile, but in the mail for our customer in Italy "File non trovato". We cannot read logfiles in italian, and not all customers can read messages in german/english. – Gabriel Gnatowski 2 mins ago
You need to refactor the interfaces so that the Write method shown get's the unlocalized message id (Message.NoFilesFound).
Then there are two approaches. Either the Logger (logger) has a member that portrays the locale ID to be used by the logger, or will be able to autonomically translate the ID to a message (perhaps, no translation at all, when logging to a database, so you can view the log in your preferred language at a later time, or simply for compression purposes).
I'm nog aware of any interpolation strings, but suspect you have them ("Fichier {0} non trouvé", e.g.) so you'd need to be able to pass these as well. A params string[]
is neat on the calling site (LoggerProxy.Write) but down the chain IEnumerable would be most versatile.
I suppose all of this is rather superficial, but I think it answers the question at least as far as I got the question clear.
精彩评论