[Enterprise Library for. NET Framework 2.0] Custom log path or log file name

Source: Internet
Author: User

Sometimes, the log output will be sorted according to the time, such as "20140821\trace.log", in Enterprise Library through the tool configuration, can only define the log file name, You can modify the Flatfiletracelistenerdata implementation by code or the custom Trace listener way,

The code to modify the Flatfiletracelistenerdata implementation code is as follows:

         Public Static stringGettracelogpath (stringListenersname) {string_tracepath =string. Empty;if(!string. IsNullOrEmpty (Listenersname)) {Configuration _etlconfig = Configurationmanager.openexeconfigura                tion (configurationuserlevel.none);                Loggingsettings _loggingsettings = (loggingsettings) _etlconfig.getsection (loggingsettings.sectionname); Flatfiletracelistenerdata _listeners = _loggingsettings.tracelisteners.get (listenersname) asFlatfiletracelistenerdata;if(_listeners! =NULL) {_tracepath = _listeners.                FileName; }            }return_tracepath; } Public Static voidSettracelogpath (stringListenersname,stringFileName) {if(!string. IsNullOrEmpty (listenersname) &&!string. IsNullOrEmpty (fileName) {Configuration _etlconfig = configurationmanager.openexeconfiguration (                System.Configuration.ConfigurationUserLevel.None);                Loggingsettings _loggingsettings = (loggingsettings) _etlconfig.getsection (loggingsettings.sectionname); Flatfiletracelistenerdata _listeners = _loggingsettings.tracelisteners.get (listenersname) asFlatfiletracelistenerdata;if(_listeners! =NULL) {_listeners.                    filename = filename;                _etlconfig.save (); }            }        }

Test code:

            Try{string_listenname ="FlatFile TraceListener"; Settracelogpath (_listenname,string. Format (@ "{0}/trace.log", DateTime.Now.ToString ("YyyyMMdd"))); LogEntry log =NewLogEntry (); Log. Categories.add ("Warning"); Log. Title ="Test"; Log. Message ="Log log Contents";                Logger.write (log); LogEntry log2 =NewLogEntry (); Log2. Categories.add ("Warning"); Log2. Title ="Test 2"; Log2. Message ="Log log content 2";                Logger.write (LOG2);            Console.WriteLine (Gettracelogpath (_listenname)); }Catch(Exception ex) {Console.WriteLine (ex.            Message); }finally{Console.ReadLine (); }

Test results:

I hope it helps.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.