I have a requirement where i need to change the log location dynamically depending on the parameter received from command line. Like i will be receiving $LogLocation parameter from another input and now i have to generate my logs in location "$LogLocation/test/log"
I am using log4j2.
Related
Calling methods for the log levels will log to a logarchive file (according to their persistence behavior) which we can view and filter in Console.app. They will also log to the Xcode debug console, displaying whatever message we handed the method, along with a giant prepended including the timestamp, app name, and category.
Is there a way to modify the output seen in the debug console to only include our message while still using Logger--specifically for iOS?
I have a Serilog implementation on a C# application. While debugging in our development environment we like to have the log file open in a text editor, like Notepad++.
As I debug and test I like to clean the log file as I go by deleting all the text and saving the file - while the application is still running.
When the application then next writes a log entry, the log is filled with 'NUL' characters.
It is as through Serilog wants to continue to append the log at the last known index point.
I just want it to write cleanly at the start of the log file again.
Is there is a way to do this?
I don't recall having this outcome when using Log4Net.
My organization is migrating from Log4j1 to Log4j2. We have a custom rolling file appender that changes the filename that it logs to at runtime when a certain event occurs in the application. This is implemented so that it's easy to find the log file in the log directory. For example the log file directory might look like this;
mylog-2021-08-02.log
mylog-2021-08-03.log
SPECIAL_EVENT_mylog-2021-08-03.log
mylog-2021-08-04.log
mylog-2021-08-05.log
Based on the research I've done it appears that Appender filenames are immutable and I'd have to create a new Appender and add it to the configuration when the event occurs, then when the triggering policy is signaled remove this Appender and add back a new Appender for the original configuration? Is there a more elegant solution than this? Do I need to write a custom appender and handle the file naming/rollover logic myself?
Update 9/2/2021
Thanks for the answer #D.B. this helped me learn quite a bit about Log4j2. The question that you reference is very similar to my situation. We have many devices, and each device needs to log to its own file. I do have some additional requirements though. We have many threads in each device which need to log to the same device log file and many devices that each need their own log file. Additionally, I need to handle the special rollover file naming requirement (original post) when a particular event occurs in the device. Finally, the name assigned to each device is not known until runtime (its defined in another configuration file we have).
I could use markers, like you suggest, but this can quickly become difficult to maintain since developers would need to know they have to pass a marker with every logging statement and the entire existing code base would need to be updated to pass the appropriate marker. I also could use a context map as you suggest but the application has many threads and again developers would need to know they have to set the context data appropriately before logging from any thread.
With Log4j1 these requirements were met by:
A custom appender class derived from RollingFileAppender that handled the special event file naming rollover logic.
A custom filter that accepted events that met the following criteria:
a. The thread name the event came from included “device name” of the device
b. The event message included “device name”
When a new device is instantiated in the system:
a. A new custom filter is created with the “device name” string to filter on.
b. A new custom appender is created that logs to a file named “device name”.log. This appender is created with the custom filter.
c. The appender is added as reference to the Root logger
This results in all log events being sent to the new appender (and every other device appender that is created) but the log events are filter based on the “device name”. This results in a device specific log file.
I could implement a custom filter and appender like we did with Log4j1 but I’d prefer not to be dependent upon the logging core classes. Any additional recommendations you have would be greatly appreciated.
You can achieve it by using ThreadContext.
My configuration file is as following
<Routes pattern="$${ctx:logName}">
<Route>
<RollingRandomAccessFile name="Rolling-Random-Access-File-Appender" fileName="${ctx:logName}.log" filePattern="${ctx:logName}.log.%d{yyyy-MM-dd-hh-mm-ss}.gz">
<PatternLayout pattern="%msg %n"/>
<Policies>
<SizeBasedTriggeringPolicy size="50 MB"/>
</Policies>
</RollingRandomAccessFile>
</Route>
</Routes>
Put some thread context where you write your log like following
ThreadContext.put("logName", fileName);
log4j2logger.log(level, logMessage);
We are trying to switch completely from log4net to Serilog. However, this part of functionality seems to be missing. What I need is to be able to get location of log-files Inside a library class. This is important for our Desktop Click-Once application because that location is different on different OSes and for different users. When user needs access to the logs we can direct him to the proper folder.
Very similar question was asked here:
Read current Serilog's configuration
But I can't believe that there is no way to get this information from Serilog. I don't need to change that configuration - just read it. In log4net we could do:
log4net.LogManager.GetAllRepositories()
and then
repository.Root.Appenders.OfType<FileAppender>
Please tell me that there is some kind back-door to the current LoggerConfiguration or if there is some alternative way to get file-path of the current File-Sink.
Serilog does not expose the list of Sinks that have been configured, so your only option at the moment would be to use Reflection if you really want to get this information from the live Serilog configuration.
You can see an example on this answer:
Unit test Serilog configuration
That said, given all you want to do is to know the path where log files are being written, that's something you can easily store at the start of the application at the moment you set up your Serilog logging pipeline.
If you configure the file path in code, you can store that information in a static property somewhere your entire app can access. If you get your folder path from the appSettings.json or App.config, you can read the information from there.
If you have environment variables in your configuration you can get the same values that Serilog gets by expanding these environment variables e.g. Environment.ExpandEnvironmentVariables("%LogPath%\\AppName.log")
Can I open the ads_err table from a windows service?
Yes. You should be able to open it just like any other table. You don't mention what development environment (client type) you are using, so I am not able to give more details specific to your situation.
After you have a connection to the server, all that is necessary is to supply the full path to the error log. If you don't want to hard code the path (probably desirable to avoid that), you can retrieve it with sp_mgGetConfigInfo(). The Error Log Path field is the one you would want.
You could also read the error log with SQL by including the path. For example, select * from [c:\ads_err].